VS2010, Linking question

A528d11e2590c45b74a53dadde386125
0
Xcrypt 101 Jan 16, 2012 at 23:31

We can link .lib files within project properties->linker->input
But can we also link .cpp files in the same way (or another)?
Because I’ve seen many errors occurring by people including a .cpp file from a library twice, resulting in a multiple definition error.

It would be handy to do this via the linker, if this is possible.

Any ideas?

12 Replies

Please log in or register to post a reply.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Jan 17, 2012 at 00:55

You cannot link .cpp files, but you can link .obj files, and compile a .cpp to an .obj. Furthermore, a .lib is nothing more than a collection of .obj’s, but has the added feature that the linker only selects those .obj’s from the .lib that contain as of yet unresolved external symbols.

I don’t really get the point you’re trying to make, though. Multiple definition errors are either 1) generated by the compiler when it encounters a definition (not a declaration) of the same symbol twice, or 2) by the linker when the same symbol is defined in multiple .obj files. You don’t really “include a .cpp from a library”, and you don’t generally #include a .cpp in code either (rather, you’d #include its header containing only declarations, and add the .cpp to the project)

A528d11e2590c45b74a53dadde386125
0
Xcrypt 101 Jan 17, 2012 at 01:38

I completely understand your confusion.
Someone I work with has made a class that I’m supposed to use, it has a cpp and a header file. However, that class is derived from another class. The definition of the parent class is is inside another cpp file.

In the child class the definition of the parent class is not available, so that person “solved” it by including the parentclass.cpp in the childclass.cpp.
Which works… Until you make another class derived from the parentclass. You can’t include it there, since you would get a multiple definition error.

The parent class is really supposed to be part of some library, so I guess the better solution would be to make the .cpp file into a .lib.
But I’ve seen other libraries (like Nvidia PhysX) making separate .cpp files in this fashion as well, so I thought there might be a common way?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Jan 17, 2012 at 01:58

Well, the real solution is to move the definition of the parent class into a header file, and #include that wherever it’s needed. Is that not possible for some reason? This problem seems completely orthogonal to the issue of linking with .objs vs .libs.

A528d11e2590c45b74a53dadde386125
0
Xcrypt 101 Jan 17, 2012 at 04:05

It is possible. But I’m just curious if there was a way to do it like this, especially since Nvidia PhysX seems to do the same thing with some definitions. (If you have it: SDK/NxCharacter/src). Or is that just because they want to show the implementations to the user?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Jan 17, 2012 at 04:23

I don’t have the PhysX SDK, but putting all class definitions in a header file is the standard thing to do in C++. You don’t have to put the implementations of the methods in the header, but you do have to put the class and all its fields in the header, if you want the class to be visible at all to other .cpp files. This holds true whether the class is part of a .lib or not. I guess I’m confused as to what alternative “fix” you are considering, since if the class isn’t defined in a header file, how does it become visible at all to other .cpp files that want to use it?

A528d11e2590c45b74a53dadde386125
0
Xcrypt 101 Jan 17, 2012 at 04:31

The class is defined in the header. But the methods aren’t.
I forgot to tell that the .cpp file of the parent class is not included in the project, maybe that is what got you confused, since like this you’d have to put the method definitions in the header, or use an alternate method to avoid multiple definition errors.

EDIT: not sure if “included” is the right term to use, since I am not referring to #include. I mean that the compiler won’t make it into a separate .obj file.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Jan 17, 2012 at 05:33

Well, that’s odd. Either the method bodies should be in the class definition (in which case they’re treated as having internal linkage, thus won’t cause duplicate definition errors), or the .cpp file containing the method bodies should be linked into the project.

A528d11e2590c45b74a53dadde386125
0
Xcrypt 101 Jan 17, 2012 at 07:05

Yes, that is odd :)
Did you just say the .cpp file can be linked into the project? Or did you mean the .obj file? Because I tried doing the standard procedure (C++->Linker->etc) with .cpp and it didn’t work.

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Jan 17, 2012 at 10:24

No you cannot link in cpp files, they have to be compiled.

Okay say you have a bunch of files you want to group into a library.

You create a new project for them, add the cpp files and the header files, compile them into a library.

In project two you link against the compiled library. When you need to create new classes based on a base class which is in the library, you include the header file for the base class in a cpp file in project two.

If you get multiple definition errors, this is usually caused by people being lazy and putting method definitions inside the header file instead of in a separate cpp file.

class Fred
{
public int calc(int x, int y){return x+y;}
}

This is legal in a header file

class Fred
{
public int calc(int x, int y);
};
int Fred::calc(int x, int y)
{
return x+y;
}

Will give multiple definition errors.

A528d11e2590c45b74a53dadde386125
0
Xcrypt 101 Jan 17, 2012 at 11:58

Well (except for the first sentence) that was not really what I asked :P . But I didn’t know that either. I suppose this doesn’t hold true for templates though? Since a lot of people define their template funcs like that, never had any problems.

6eaf0e08fe36b2c23ca096562dd7a8b7
0
__________Smile_ 101 Jan 17, 2012 at 13:25

Template = inline, so you can put template and inline functions in header file without linker errors. You can also move all functions from *.cpp file to *.h and put inline before every function.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Jan 17, 2012 at 17:03

By “linking the .cpp file into the project” I mean adding it to VS project file, so that it gets compiled when the project is built and its .obj linked into whatever the project is building.