In my experience the appropriate way to include external class files in C++ is like so:
-Where the interface is declared in the header file (ClassTest.h) and the implementation is defined in the source file (ClassTest.cpp). For some reason though, when I try to compile my main source file (main.cpp) like so:
$ g++ main.cpp
I get the following error:
Undefined symbols:
"ClassTest::test()", referenced from:
_main in cciBbM2p.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Strangely, if I include my external source like so:
everything works fine! Obviously this isn't the end of the world because it does in fact work, but I am confused about why I can't include the .h file in the standard fashion. Am I missing some compiler flags or just doing something silly? Here is my complete source just in case:
----------
main.cpp
----------
-------------
ClassTest.h
-------------
---------------
ClassTest.cpp
---------------
Code:
#include "ClassTest.h"
-Where the interface is declared in the header file (ClassTest.h) and the implementation is defined in the source file (ClassTest.cpp). For some reason though, when I try to compile my main source file (main.cpp) like so:
$ g++ main.cpp
I get the following error:
Undefined symbols:
"ClassTest::test()", referenced from:
_main in cciBbM2p.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Strangely, if I include my external source like so:
Code:
#include "ClassTest.cpp"
everything works fine! Obviously this isn't the end of the world because it does in fact work, but I am confused about why I can't include the .h file in the standard fashion. Am I missing some compiler flags or just doing something silly? Here is my complete source just in case:
----------
main.cpp
----------
Code:
#include <stdio.h>
#include "ClassTest.h"
ClassTest tester;
int main(void)
{
tester.test();
return 0;
}
ClassTest.h
-------------
Code:
class ClassTest
{
public:
void test(void);
};
---------------
ClassTest.cpp
---------------
Code:
#include "ClassTest.h"
void ClassTest::test(void)
{
printf("hello! \n");
}