How linker solves ambiguities when linking *.libs? - c++

after very long long time spent with unresolved externals I found that VS2010 was linking wrong .lib file.
Files were unfortunately named with same name.
Folder with linked(unwanted) lib was set in project properties
Folder with unlinked(wanted) lib was inherited from other property sheet
Desired lib was newer date, undesired was very older.
Linker chose the unwanted one (as above).
What are the rules for linking libs?
Shouldn't linker give at least warning on some ambiguity?
edit:
symbol is defined ONLY in NEW lib. And the NEW lib is being searched only when I delete OLD lib (no linker settings changed)

Think of LIBs to Linker as header to compiler. A LIB only has signature of exported symbols, unless it is static LIB. LIB files do not have any version attached to them.
Two LIB file can have same name, as two header files can have. It's you, the programmer, who has to use correct LIB/header file. If the linker doesn't find all symbols, or if symbols are not correct, it will report error (just like compiler will do for inconsistent symbols in header file).

Related

Adding external library (Gurobi) to Eclipse C++

I would like to use Gurobi in a C++ project I have in Eclipse. I tried multiple manuals/tutorials (including THIS on how to do the same in Visual Studio) to add the hook up the Gurobi files with Eclipse/GCC, but I just couldn't make it work. I feel like I don't understand enough how these things should work in the first place.
In my Gurobi folder I have 3 folders, that in my opinion are important: include (with .h files), lib (with .lib files and NO .so or .a files) and src (with .cpp & .h files).
include: I added the path to this folder to C/C++ Build -> Settings -> GCC C++ Compiler -> Includes -> Include paths, I guess this is for the compiler to see the .h files. I also added it to C/C++ General -> Paths and Symbols -> Includes -> Include directories, I guess this is for the linker to see the .h files.
lib: I first added the folder path to C/C++ Build -> Settings -> MinGW C++ Linker -> Libraries -> Libraries (-l), however after searching online it seems to me this option only works for libraries with standard names and extensions. It also gave me the error ld.exe: cannot find -lc:/gurobi900/win64/lib/, so I deleted it and added the two .lib file paths separately to Miscellanous -> Other objects.
I got to the point where #include "gurobi_c++.h" works, however, already Eclipse underlines a line where I am defining a GRBEnv() object, that is declared in include/gurobi_c++.h, but defined in src/cpp/Env.cpp. Also if I try to compile, I get undefined reference errors. This means neither the Linker nor the Compiler can see the definition of GRBEnv().
Is there a way to tell the linker/compiler where the .cpp files are? Is it happening automatically, based on the path of the include folder? Am I supposed to link the .lib or the .cpp files?
I would greatly appreciate even if someone just pointed out mistakes in my understanding.

C++ LNK2019 Error, lib has been added to linker

First time I actually started writing a project in C++, for purposes. I'd like to include the TNL library.
The header files are already defined correctly and can use them. For the linker, when i try to use a function that's in the lib file it still gives me a LNK2019 error. The lib is added to the linker.
Error message example:
Here are some screenshots with the paths. I 'think' that i've put them correctly but could definitely looked over something.
Additional Dependencies:
Additional Library Directories
Actual Library Lib's are located at:
I'm probably tunnel-visioning on something. But I'm stuck right now and I'm clueless.
The unresolved symbol is _closesocket#4, which should probably resolve to closesocket() from the Windows sockets library. Try adding "Ws2_32.lib" to the Additional Dependencies line.

linking error for not being able to link header file to cpp file correctly

I have two projects in one solution. When I try to access a function of one project's from another one I get error LNK2001: unresolved external symbol. But the linking error only happens when the function is declared in the header file but defined in the corresponding cpp file. If the function is defined in the header file than the error does not occur. Also calling function from same project doesn't give any error. Only calling from another results on linking error.
EDIT: I am using visual studio 2010. I don't know if its worth mentioning that the function which is being called is in a project that outputs a lib file and the one which is calling that function outputs a exe file.
If you define the function in the header file, the compiler will see the function implementation when you build the .exe project and compile a copy of the function code directly into your .exe project. When it is the linker's turn during the build, nothing is missing so the linker is happy and you won't get an error message.
If you define the function in the .cpp file, the compiler will not see the function implementation. It will thus put a reference to the function (i.e. the external symbol) that needs to be resolved later on when it is the linker's turn during the build. To make the linker "see" the external symbol, you need to link your .exe project against your .lib project. Once you have established this link dependency, the linker will be able to find the external symbol and resolve the reference to the function that was earlier generated by the compiler. Because you have a .lib project, which is a static library project, the linker resolves the symbol by grabbing the code for the function from the .lib file and places a copy of the code into your .exe file.
So much for the theory. Now the simplest way to make your .exe project link against your .lib project probably is by adding a reference:
In the .exe project's settings, select the section named "Common Properties" at the top of the section list.
You should now see a list of references that the .exe project has. The list is probably empty.
Click the "Add new reference" button at the bottom of the dialog and add the .lib project as a reference
When you select the new reference in the list of references you will see a set of properties for that reference. Make sure that the property called "Link Library Dependencies" is set to true. This will cause the .lib project to be added automatically as an input to the linker when you build the .exe project.
If you build your .exe project, the linker error should now be gone.
Incidentally, by adding the project reference you have also told Visual Studio to build the two projects in the correct order if you build the entire solution: First the .lib project, then the .exe project.
It is the role of the linker to resolve unknown symbols.
Thus if projet A uses methods from projet B defined in the cpp file, you need to link A against B.
As stated, it would be fine to have more information about both projects, IDE (visual???)...

avr linker++ setting header and .cpp files

I want to set the linker correctly so an eclipse project can work properly.I have added the the folder that my header and cpp files exist to the c/c++ compiler directories. I have to set the linker also. I do n ot know how cause the linker in the tool settings wants to add a library (an .a file that does not exist cause none makes it), so since i do not have any .a file i get error at the functions cause i can give the folder path to the linker but inside there there is no .a file. What can i do.

Identifying Needed Link Libraries

I'm using C++ under Visual Studio 2010 (VS2010). I've pulled in a third-party library provided as headers and libs. The first call I added into this library produced a ton of unresolved symbol link errors. The problem is that there are a lot of .lib files in this library. I need to somehow identify the ones I need. I have already added the library directory to the VS2010 linker options. Now I just need to identify the individual libraries I need.
Best possible solution: After the link attempt, VS2010 would scan the library directory for the unresolved symbols and would tell me what .lib files contain them.
Barring this, I'd like to solicit ideas from the community on the most efficient way to solve this problem.
Thanks,
Dave
The documentation is supposed to tell you. If you don't have any then leave it up to the linker to sort it out. Add all the .libs as Additional Dependencies.
Then take a lazy Friday to sort out which ones you really need with the /VERBOSE linker option, it shows you which .lib is actually getting used in the trace to the Output window. Project + Properties, Linker, Command line to add the option.