I have the following question: I was given the task - to build an application. There was a ready file counter.h and some other file - counter.obj. It turned out that in the counter.h there were only declarations of the functions - how can I include .obj file into the .cpp file so that it compiles? I am using Microsoft Visual Studio 2010 - and in which folder should the file itself go?
2 Answers
Add the obj-file to the Solution just as you would do with cpp-files (i usually do this by drag-and-drop, that is, drag the file from the Windows Explorer and drop it on a project in the Solution Exporer window).
You can put the obj-file together with cpp-files; it doesn't really matter.

- 26,506
- 9
- 60
- 134
You do cannot include object file in to a cpp file.
The compiler compiles the cpp file and generates the obj files, for each cpp file, these files are further linked together to create an libray or an executable.
Usually, you would link libraries(.lib
or .dll
) to an Application, Check if those are with you.
If not,
You can try linking the object file to your application by:
Go to project properties then from "Property Page"
select the node "C/C++"
their you will get "Additional Include Directories"
add the name of your object file.Keep your obj file in the directory where your source code is or you can add the directory from:
Tools->Options->Projects and Solutions->VC++Directories
.
I have never tried the second method except for academic projects,which was years ago, So not sure about it, Please check information on MSDN.

- 202,538
- 53
- 430
- 533
-
But what shall I do then if I have only the declaration of class Counter in Counter.h but no definition of its function! THe teacher gave me additionally Counter.obj - how can I start the program - I dont really need to know the implementation of the class, but I need my cpp to compile - and the Compiler gives: error LNK2019: unresolved external symbol "public: __thiscall Counter::Counter(int)" (??0Counter@@QAE@H@Z) referenced in function _main – Artem Moskalev Oct 30 '11 at 17:28
-
@ArtemMoskalev: That is a **Linker error** because linker could not find the definition of the function which is in object file,try the method I updated in answer. – Alok Save Oct 30 '11 at 17:30
-
fatal error LNK1120: 1 unresolved externals - still the same it does not find it! Maybe the mustake is in how i show the path to the file? I put it into the directory where the main.cpp is and in linker wrote: counter.obj – Artem Moskalev Oct 30 '11 at 17:48