0

The problem is specific to VS2010. I couldn't reproduce it in a simple case to give an MCVE (see below).

There is some old, legacy project. It has (among others) 3 C++ projects:

  1. GoogleTestingFramework - a .lib project that delivers features from GTest and GMock. Created for usage simplicity.

  2. OldLegacyProject - an old .exe project.

    • The entities on interest here are Class1 and Class2. Both are in separate files, Class1.cpp+Class1.hpp (the same for Class2).
  3. OldLegacyProjectUnitTests - a new .exe project with Unit Tests written using GTest and GMock.

When I try to build everything, I get following liker errors:

3>OldLegacyProjectUnitTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Class1::Class1(class Class2 &)" (??0Class1@@QAE@AAVClass2@@@Z) referenced in function "private: virtual void __thiscall SimpleCase_Should_CreateNewInstance_When_Called_Test::TestBody(void)" (?TestBody@SimpleCase_Should_CreateNewInstance_When_Called_Test@@EAEXXZ)
3>OldLegacyProjectUnitTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Class2::Class2(void)" (??0Class2@@QAE@XZ) referenced in function "private: virtual void __thiscall SimpleCase_Should_CreateNewInstance_When_Called_Test::TestBody(void)" (?TestBody@SimpleCase_Should_CreateNewInstance_When_Called_Test@@EAEXXZ)

3>../Debug/OldLegacyProjectUnitTests.exe : fatal error LNK1120: 2 unresolved externals

I created a simple example using the guide: Google Test #1.b: Setup Googletest in Visual Studio, with the same configuration as above, and it works correctly.

The questions are:

  • Why doesn't the linker see those classes despite the Project Reference?
    • How to see what the linker actually "sees"? (at least, which files)
  • Consequently: how to feed the classes to the linker?

If I explicitly add a path to those .obj's, everything compiles and links correctly. But, this is a very convoluted way to go, and I want to give those who will deal with this relatively simple instructions.

I will appreciate any help.

Community
  • 1
  • 1
Sokeks
  • 47
  • 8
  • Voting to reopen: http://stackoverflow.com/questions/25611998/using-libpq-with-visual-studios-2013-compiling-to-a-dll is a better candidate for duplicate marking since it shows how to link to libraries in VS. – ivan_pozdeev Oct 11 '15 at 19:40
  • I am against reopening - it is not trivial linking of .libs, my question is different. My Old legacy project is and .exe one. Using references instead of modifing linker parameters was very helpful for me to work with .obj files (NOT .lib files), that are intermediate during .exe creation... and that's the problem, why it does not work! – Sokeks Oct 11 '15 at 21:06
  • You are not supposed to link a project with single .obj files from another project. Refactor the shared code into a static library. This "dirty hack" may have worked for some reason, but it's definitely not a supported way to do it. – ivan_pozdeev Oct 11 '15 at 21:16
  • For why it worked, my guess is that before, referencing a project added all its object files to the referrer's link path, but now, it only adds its final artifacts to it. The reason probably was to fix incorrect linking in the case of name clashes. – ivan_pozdeev Oct 11 '15 at 21:30
  • I am not able to refactor it - as I said it's old and legacy code and noone wants to do more changes than absolutely necessary. And why should I not use .obj files? Any reason for that? I use the same compiler, it's compiling one after another, so it shouldn't be a problem. Additionally this one of the proposed solutions, e.g. look here: [Using .obj's](http://stackoverflow.com/a/23138390/867191). The only different thing is using References, what seems not working for me :( – Sokeks Oct 11 '15 at 21:52
  • And the final phrase on the link is: _"Maintaining the right linkage of UnitTest could become a chore, and you might come to the conclusion that the drill-sergeant was right after all."_ Fine, let's mark this question as a duplicate of that one, I don't care as long as I can wholeheartedly call this case solved. – ivan_pozdeev Oct 11 '15 at 22:02
  • Due to this, I still advise the library since it will _save_ you work in the long run. – ivan_pozdeev Oct 11 '15 at 22:12
  • @ivan_pozdeev Man... with all due respect, but you don't get what I ask for... I know the possible situation, workarounds, etc. but because of some other conditions I am looking for simples possible solution for my case. It SEEMED to be using Visual Studio REFERENCES from Framework and References, but it doesn't work for my particular situation. I don't know why it does not work and looking for help how to solve or debug the linker's behaviour. Why do you force so much to close this question? – Sokeks Oct 11 '15 at 22:16
  • ...Otherwise, you need to do "tambourine dances" to add you "supposedly-linked" files to linker input. Referencing didn't do this, so we now need to find out what exactly it does and, consequently, what else we need to adjust. Show or study 1) The linker's command line 2) the directory your "supposedly-linked" classes (.obj) are at 3) the build log to see if something from OldLegacyProject is copied somewhere as a result of the reference. – ivan_pozdeev Oct 11 '15 at 22:16
  • Besides, _"if I explicitly add a path to these objects, everything compiles and links correctly."_ - isn't that your "simplest solution"? That's exactly what we need to do. The questions in the prev. comment are intended to do this in a more "VS-esque" manner, but if you don't care... – ivan_pozdeev Oct 11 '15 at 22:20
  • @ivan_pozdeev No, the Reference can work and I have a working solution showing that it works (I have written about it), but there must be some corner case it does not work in other case. And 1) I studied and cannot find any pattern difference between my working and non-working solution 2) the build log, it's good direction, tried it, but I cannot find any tips what should or shouldn't be ther. – Sokeks Oct 11 '15 at 22:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91985/discussion-between-ivan-pozdeev-and-sokeks). – ivan_pozdeev Oct 11 '15 at 22:22
  • @ivan_pozdeev "isn't that your "simplest solution"?" - no, I would like to give other users simple manual to repeat my steps, like "add reference and everything will work out of the box", adding explicitly every .obj is not that simple and quick. – Sokeks Oct 11 '15 at 22:23
  • @ivan_pozdeev Sorry, I cannot access chat right now - company's filters are blocking chats:( – Sokeks Oct 11 '15 at 22:24
  • Try HTTPS to circumvent. On the base matter: The simplest way is look for differences in build logs - specifically, linker command lines and lines related to files/dirs in question. – ivan_pozdeev Oct 11 '15 at 22:31
  • Resolution: Reference DO NOT include .obj files. The simple example I had was using header files with template functions, that are inlined from .hpp, so it did not proove anything. I had to search for general solution. – Sokeks Oct 12 '15 at 08:39

0 Answers0