I am new to C++ using vscode on MAC and I tried to test using a sample program.
In the header file, I declare the class.
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Student{
public:
string name;
int rollno;
Student();
Student(string s, int id);
void printDetails();
};
#endif
The implementation is done in the .cpp file.
#include <iostream>
#include "student.h"
using namespace std;
Student::Student(){}
Student::Student(string s, int id):name(s),rollno(id){}
void Student::printDetails(){
cout<< rollno << "-" << name<<endl;
}
Finally, I added Main.cpp.
#include <iostream>
#include "student.h"
using namespace std;
int main(){
Student students[8];
students[0] = Student("Tom", 1);
students[1] = Student("Jerry", 2);
for(int i = 0; i < 2; i++){
students[i].printDetails();
}
}
However, when I followed the procedures suggested in https://code.visualstudio.com/docs/cpp/config-clang-mac . The compiler issued errors
Undefined symbols for architecture x86_64:
"Student::printDetails()", referenced from:
_main in Main-896002.o
"Student::Student(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from:
_main in Main-896002.o
"Student::Student()", referenced from:
_main in Main-896002.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The issue can be resolved by manually linking Main.cpp with student.cpp, i.e.,
g++ Main.cpp student.cpp -o Main
However, is there a more effective way to do so as the manual work is troublesome when the project size is large?
Related
I'm trying to compile this program but I keep getting this error in the report log
Undefined symbols for architecture x86_64:
"Net::feedForward(std::__1::vector<int, std::__1::allocator<int> > const&)", referenced from:
_main in main.o
"Net::backProp(std::__1::vector<int, std::__1::allocator<int> > const&)", referenced from:
_main in main.o
"Net::getResults(std::__1::vector<int, std::__1::allocator<int> >&) const", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My code is straight from a youtube tutorial I was trying to follow, here is the main.cpp code:
#include <iostream>
#include <vector>
#include "Net.h"
int main() {
vector<int> topology;
Net myNet(topology);
//constructor needs to know # of layers it wants & # neurons per layer
vector<int> inputVals;
vector<int> targetVals;
vector<int> resultVals;
myNet.feedForward(inputVals);
//feeds inputs to begin with
myNet.backProp(targetVals);
//pass in some array with goal state
myNet.getResults(resultVals);
}
Net.h code:
#ifndef Net_hpp
#define Net_hpp
#include <stdio.h>
#include <vector>
using namespace std;
class Neuron{};
typedef vector<Neuron> Layer;
class Net{
public:
Net(const vector<int> topology);
void feedForward(const vector<int> &inputVals);
void backProp(const vector<int> &targetVals);
void getResults(vector<int> &resultVals) const;
private:
vector<Layer> m_layers; //m_layers[layerNum][neuronNum]
};
#endif /* Net_hpp */
and Net.cpp code:
#include "Net.h"
#include <vector>
Net::Net(const vector<int> topology){
int numLayers = topology.size();
for(int layerNum = 0; layerNum < numLayers; ++layerNum){
}
}
I really have no clue how to fix it, and other threads with similar-ish issues haven't helped either. If someone could please help me out I'd greatly appreciate it.
Although have some questions with the same title but, I believe that my problems are little different.
I have 3 files: main.cpp, car.h and car.cpp.
main.cpp:
#include <string>
#include <iostream>
#include "car.h"
int main(int argc, const char * argv[]) {
Car v;
std::cout << v.getName() << std::endl;
return 0;
};
car.h
#include <string>
#include <iostream>
#ifndef CAR_H
#define CAR_H
class Car {
public:
std::string getName();
};
#endif // MAINWINDOW_H
car.cpp
#include "car.h"
std::string Car::getName() {
return "blah";
};
When i run in terminal with "g++ main.cpp", i got this strange message:
Undefined symbols for architecture x86_64:
"Car::getName()", referenced from:
_main in main-b1d04e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
It's a newbie question, but could anyone help me?
I've read the other questions on this topic but still haven't figured out how to fix my issue
Thank you in advance for your help!
My error is:
Undefined symbols for architecture x86_64:
"Record::Record(std::__1::vector, std::__1::allocator >, std::__1::allocator, std::__1::allocator > > >, double*)", referenced from:
_main in akh70P3ClassTester-946637.o
ld: symbol(s) not found for architecture x86_64
Record.h
#include <string>
#include <vector>
using namespace std;
class Record
{
public:
Record();
Record(vector<string> , double []);
private:
//some variables
};
Record.cpp
#include "Record.h"
#include <string>
#include <vector>
using namespace std;
Record::Record() {}
Record::Record(vector<string> inputs, double num_inputs[] )
{
//variables happens
}
Main.cpp
#include "Record.h"
#include <vector>
using namespace std;
int main() {
vector<string> inputs;
double num_inputs[] = {};
Record temp(inputs, num_inputs);
return 0;
}
You probably aren't including Report.cpp in your compilation, e.g. only doing g++ main.cpp -o main
Instead, compile your program by including the report files: g++ main.cpp report.cpp -o main
I was trying to compile the following simple thread example for the Boost library:
#include <iostream>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
using namespace std;
void thread()
{
for (int i = 0; i < 5; ++i)
{
cout << i << '\n';
}
}
int main()
{
boost::thread t{thread};
t.join();
}
using
g++ -std=c++11 -I /usr/local/boost_1_57_0 simpleThreadExample.cpp
and the compiler gave this back to me:
Undefined symbols for architecture x86_64:
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<void (*)()>::~thread_data() in simpleThreadExample-7cec5e.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in simpleThreadExample-7cec5e.o
boost::thread_exception::thread_exception(int, char const*) in simpleThreadExample-7cec5e.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in simpleThreadExample-7cec5e.o
___cxx_global_var_init1 in simpleThreadExample-7cec5e.o
"boost::thread::join_noexcept()", referenced from:
boost::thread::join() in simpleThreadExample-7cec5e.o
"boost::thread::native_handle()", referenced from:
boost::thread::get_id() const in simpleThreadExample-7cec5e.o
"boost::thread::start_thread_noexcept()", referenced from:
boost::thread::start_thread() in simpleThreadExample-7cec5e.o
"boost::thread::detach()", referenced from:
boost::thread::~thread() in simpleThreadExample-7cec5e.o
"typeinfo for boost::detail::thread_data_base", referenced from:
typeinfo for boost::detail::thread_data<void (*)()> in simpleThreadExample-7cec5e.o
"vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base() in simpleThreadExample-7cec5e.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Now, the following two programs compile and run just fine:
#include <locale>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost::gregorian;
using namespace boost::posix_time;
int main()
{
date today = day_clock::local_day();
cout << today << endl;
}
And
#include <iostream>
#include <iomanip>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
using namespace std;
int main()
{
//point_type p = boost::geometry::make<point_type>(1, 2, 3);
//std::cout << boost::geometry::dsv(p) << std::endl;
//return 0;
boost::geometry::model::point<int, 3, boost::geometry::cs::cartesian> p;
boost::geometry::assign_values(p,1, 2, 3);
boost::geometry::model::point<int, 3, boost::geometry::cs::cartesian> p2;
p2 = p;
cout << boost::geometry::dsv(p) << endl;
cout << boost::geometry::dsv(p2) << endl;
return 0;
}
And, when I compile, I do:
g++ -std=c++11 -I /usr/local/boost_1_57_0 <nameOfProgram.cpp>
So, I'm wondering why the thread program won't compile while these other two will when the only thing I'm changing is the name of the program?
If it helps, I am using Boost 1.57 and the path to it is:
/usr/local/Cellar/boost/1.57.0
And the path to the header files is:
/usr/local/Cellar/boost/1.57.0/include/boost
If anyone could provide some insight, that would be brilliant. Thanks!
You have to link against the boost library too:
-lboost_thread
Possibly with additional -L argument for path, and likely the standard -lpthread too.
I'm having trouble understanding some errors I've been getting. I have constructed a simple Test class with a driver. Can somebody please point out the errors I have made?
Here I'm trying to create a Test object and set the number variable to 1, and then print the number variable.
driver:
#include "test.h"
#include <iostream>
using namespace std;
int main() {
Test *myTest = new Test(1);
cout << myTest->getNumber();
return 0;
}
test.h
#ifndef __TEST_H__
#define __TEST_H__
class Test
{
private:
int number;
public:
Test();
Test(int theNumber);
int getNumber();
};
#endif
test.cpp
#include "test.h"
Test() {
}
Test(int aNumber) {
number = aNumber;
}
int getNumber() {
return number;
}
The error I'm getting here is
> Undefined symbols for architecture x86_64: "Test::getNumber()",
> referenced from:
> _main in cc8cXu6w.o "Test::Test(int)", referenced from:
> _main in cc8cXu6w.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
Thank you
You should use class scope when defining a class member outside its class.
Test::Test(){
}
Test::Test(int aNumber){
//...
}
int Test::getNumber(){
//...
}
Also, don't forget to compile and link test.cpp. Compiling only main.cpp (or whatever is your driver source file called) could also lead to such linking error.
If you use GCC, use the following command to build:
g++ -o test main.cpp test.cpp