1

I've got three files, add.h, add.cpp and test.cpp

add.h creates a header for a class, and is nothing but skeletons for implementation later.

add.cpp contains the actual code for the class listed in add.h

test.cpp contains the main method and declares an instance of the class in add and uses some of its methods.

However, I'm having trouble compiling it. In add.h, I have #DEFINES to prevent multiple writes of the header, and in add.cpp and test.cpp I have add.h included, but when I attempt to compile using the line

g++ test.cpp -o test

I get an error about undefined references to the class objects and methods in add.h. I've been searching google on how to compile or run this, but so far no help, can StackOverflow help me?

EDIT: Sorry, I should have also included that I did try g++ test.cpp add.cpp -o test and it didn't work either, yielding the same resulting errors.

Nicholas
  • 7,403
  • 10
  • 48
  • 76
  • If your class is templatized `g++` doesn't always play nice with it if you're trying to keep true to `encapsulation`. You have two options if this is the case: 1 define everything in your header file (not as easy to read) or two, keep your declarations in your header and then at the end (before the `#endif` for include guards) use `#include "add.cpp"` and remove `#include "add.h"` from the .cpp file. Then try to recompile test.cpp. – RageD Apr 02 '11 at 20:37
  • That's it. Thank you for the solution. I had created this just to play around with headers and modular code, but it seems a little more complex then I had hoped for. – Nicholas Apr 02 '11 at 20:43

4 Answers4

5

Compile each file separately, then link:

g++ -Wall -c test.cpp
g++ -Wall -c add.cpp
g++ -o test test.o add.o

Or compile and link all files in one command:

g++ -Wall -o test test.cpp add.cpp
Erik
  • 88,732
  • 13
  • 198
  • 189
  • I attempt both methods and I still get the same error. test.cpp:(.text+0x1a4): undefined reference to `ArrayList::ArrayList()' – Nicholas Apr 02 '11 at 20:28
  • @Nicholas: That's another issue - see http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-the-s – Erik Apr 02 '11 at 20:30
2

run g++ test.cpp add.cpp -o test

EDIT: copypasted my comment here

You need to understand why your initial approach isn't working. When you reference stuff from add.h header in test.cpp, the compiler looks for definitions, but does not find them, because they are in add.cpp and you did not pass it to the compiler. The compiler can't just guess that it should look for the definitions in the add.cpp file just because you included add.h in test.cpp.

  • You need to understand why your initial approach isn't working. When you reference stuff from add.h header in test.cpp, the compiler looks for definitions, but does not find them, because they are in add.cpp and you did not pass it to the compiler. The compiler can't just guess that it should look for the definitions in the add.cpp file just because you included add.h in test.cpp. –  Apr 02 '11 at 20:27
  • I think the comment should be a part of the answer. – LiraNuna Apr 02 '11 at 20:29
  • Your comment and your edit confuse compiler and linker. It's the latter which needs the `add.cpp` object code as `test.cpp` refers to it. – Dirk Eddelbuettel Apr 02 '11 at 20:56
2

run g++ test.cpp add.cpp -o test

or

g++ -c add.cpp -o add.o
g++ -c test.cpp -o test.o
g++ test.o add.o -o test

the -c flag tells gcc to just compile and not link the first two steps compile 1 cpp (a compilation unit) in an object file the last step links those into a single executable

your actual problem comes from the fact that when you compile test.cpp, it refers to some simbols which are undefined. If you're just compiling (-c flag) that's fine, and the next step is to link with those objects file containing the missing symbols.

f4.
  • 3,814
  • 1
  • 23
  • 30
1

You need

 g++ test.cpp app.cpp -o myTest

as app.cpp contains code used by test.cpp.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725