Good day! Such a problem, I can not connect the class and create a binary.

test.h #ifndef _test_h_ #define _test_h_ class test { public: test (); void show(); private: }; #endif 
 test.cpp #include "test.h" #include test::test(){} void test::show(){ std::cout 
 main.cpp #include "test.h" int main (int argc, char const* argv[]) { test t(); t.show(); return 0; } 

g ++ -o bin main.cpp displays /tmp/ccmd32Df.o: In function main ': main.cpp :(. text + 0x2c): undefined reference to test :: test ()' main.cpp :(. text + 0x3b ): undefined reference to `test :: show () 'collect2: error: ld returned 1 exit status

1 answer 1

The fact is that you compile only main.cpp , but not test.cpp , and accordingly, when linking, the linker does not find the function test::test() , and reports to you about it. For good, you should compile each .cpp file into an object file ( main.cpp -> main.o ), unless the .cpp file is connected via #include , and then all the object files are linked into the executable. That is, in your case, the team will look like this:

 $ gcc -c main.cpp -o main.o # -c говорит GCC создать из файла исходного кода объектный файл $ gcc -c test.cpp -o test.o $ gcc main.o test.o -o bin 

Or you can immediately transfer to the compiler all .cpp files and he will do everything himself:

 $ gcc main.cpp test.cpp -o bin