Linking .obj files

Alright, let me see if I've got this right: If I have

a header file named, say, "test.h" which contains the

following declaration:

void test();

and a file named "test.cpp" which contains the


#include "test.h"



void test()


printf("This is a test");


I can simply compile test.cpp, thus creating an obj file

in a particular directory, and then have yet another

file, "testi.cpp" including only the test.h file and

still be able to call the function test(), as long as

it is linked to the obj file and outputs to the same

directory, can't I? Unfortunately, I get the linker

error "Undefined symbol test() in module testi.cpp."

Am I doing something wrong? Thanks for your help.



  • The simplest way to have a multi-file project is to make sure both source files are in your project and to make sure that the prototype (in the header file) is visible to the caller source (by doing a #include).

    It is also often helpful to #include the header in the source for the function itself so that the prototype and the definition don't get out of synch. So put a #include at the top of your source file with the call to the routine as well as the top of the source file with the called routine.

    If you're using Borland with the GUI project environment, you shouldn't have to link in the obj separately. Just include both .cpp files in the project. It will do the appropriate linking.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!