xcode: difference between adding a linker flag and "link binary with libraries" - c++

I switched a few days ago from windows and I find a bit confusing the linking process in xcode.
I have a static library I want to use called nag C library whose file is "libnagc_nag.a".
If I add "/opt/NAG/clmi623dgl/lib/libnagc_nag.a" to "other linker flags", the program runs perfectly.
On the other hand, If i go to "Build Phases -> link binary with libraries -> + -> add other" and add "libnagc_nag.a", the library file now appears in the project navigator but I get the following error after the build succeeds and it tries to run it.
dyld: Library not loaded:
/fserver/mo/cl23/CL23.3/CLMI623DG_build/BUILD_mac_64/climp/LIBS/nag_lm/libnagc_nag.dylib
Referenced from: /Users/german/Library/Developer/Xcode/DerivedData/myproj-bjepqyzlzkayspddtaspmmzxgaid/Build/Products/Debug/myproj
Reason: image not found
my questions are:
Why is it any different?
does it think it's a dynamic library?
How can I solve this problem in the second case?

Edit: I removed a link to the article where I found the answer since it was outdated.
The problem is that the linker flag is the same for both, dynamic and static libraries, and the file extension is removed from it. So, if you have a static and a dynamic library with the same name and only different extensions, the linker will chose by default the dynamic version of the library. This happens even if you chose the the static version in the "link binary with libraries" menu. Then, if the app can't find the dynamic library there will be an error. So, the answers to the question are:
1) it wouldn't make any difference if there wasn't a dynamic library with the same name. 2) The linker only know the name of the library, so by default it choses the dynamic version. 3) The solution can be renaming the library or using the first method.

Does your project have multiple build targets? If so, did you add the library to the appropriate target? Check this by looking at "link binary with libraries" again or by selected the framework in the Project Navigator and looking at "Target Membership" in the File Inspector pane.
Also, is your framework search path set correctly? Click on your project at the top of your project navigator, then click on "Build Settings" in the top center of the screen, then go down to "Search Paths" and make sure an appropriate path is included.

Related

How can I install and use C++ libraries on eclipse?

I want to use libosmium library. Could someone please tell me how do I set up this library after I download it?
All I need to know is the standard way of installing external libraries. I can't really find very clear instructions online.
I'm coding with C++ using the eclipse IDE version 4.18.0.
For includes: right click on your project and go to Properties>>C/C++ Build>>Settings>>GCC C++ Compiler >> Includes
you must add the include path for the external library (where the header reside). This information is needed by the Eclipse indexer (code completion etc.) and the compiler
For libraries: right click on your project and go to Properties>>C/C++ Build>>Settings>>GCC C++ Linker >> Libraries you must add the library search path (option -L) and the library you want to link against (option -l). This info is needed for the linker.
Source: http://wiki.eclipse.org/CDT/User/FAQ#How_do_I_add_an_external_library_to_my_C.2B.2B_project.3F
Eclipse-CDT Setting Pictures
Include path settings:
Library & library search path settings
You can also use pkg-config plug in
https://marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
Here is a link to a similar question with answers: Problems importing libraries to my c++ project, how to fix this?
As specified in the other answer, you can usually add a library by looking at the project properties:
Properties>>C/C++ Build>>Settings>>GCC C++ Compiler>>Includes
However if you're using a makefile project, the Makefile itself must know about the location of libraries. In this case, in order to avoid an "Unresolved inclusion" warning in the header, you may also want to tell eclipse where the header files are. This way the autocomplete & "Open Declaration" will work. This is found in:
Properties>>C++ General>>Preprocessor Includes Paths, Macros etc.
In my case I couldn't figure out how to add a custom configuration and updated the default configuration. I added a path for the Android NDK under the GNU C++ -> CTD User Setting Entries

ftd2xx static library link error

I'm working on a DLL project in Visual Studio 2017. This library adds some functionality around the ftd2xx library from FTDI. I set up the project as described in this instruction. I'm linking against the static version of the ft2dxx library. The only difference between the instruction and my project is the selection of the Runtime Library. The instruction states (figure 2.16) that the Multi-threaded Debug Runtime library must be used. In my case, I selected the Multi-threaded Debug DLL (/MDd) option, because i'm working on an DLL (see the image below)
When i build the project, I get the following errors:
When i clicked on the first warning, this page opens. I changed the project properties as described on the webpage:
This fixes the first warning, but the link errors still exists:
So, to conclude: I assume there is something wrong with the Runtime Library, used by the ftd2xx library, but I have no idea how to fix this. Any suggestions?
I found the solution here. The solution is to add the 'legacy_stdio_definitions.lib' to the linker input. To do this, go to the project properties -> Linker -> Input and add 'legacy_stdio_definitions.lib' to the additional dependencies.

How to specify the library version to use at link time in eclipse?

I have a C++ project in Eclipse Juno (Service Release 2) on Linux.
I need to reference a third party library, but its name includes the library's version:
libThirdParty.so.10.1
I tried following this post. But it seems I have an old version of ld, and it doesn't work.
(I checked the man page)
I tried using the library's full name (path/name) but I don't know where to add it because it is not working.
I can not create a symbolic link of the library without the version.
In eclipse, Where should I reference the full library name? Is there another way to reference the library?
In the project explorer select your project and press Ctrl + Enter. This will popup the properties window.
Then, under C/C++ -> Settings in the Tool Settings Tab select Linker -> Libraries click on the button add and write the library name-version you want to link to.
Warning: Be aware that in a Linux system exist a thing named soname for shared libraries, read this answer for a detailed explanation about it.
Edit: To the comments
The thing is the linker will look for a file named lib<name>.so since your library's name is libThirdParty.so.10.1 and don't ends with .so don't match this pattern. This is not a problem you can solve with eclipse, this is a general linux behaviour. If your library name were libThirdParty.10.1.so (note the trailing .so) then you could use ThirdParty.10.1 as the library name in the libraries eclipse field.
Linux use this name convention in order to make easier handle various version of he same lib installed in the system. What you want to do can't be done without use a symbolic link or rename the library to libThirdParty.10.1.so.
I coworker helped me to solve this:
Right Click on the Project => Properties => C/C++ Build => Settings
Under GCC C++ Linker => Miscellaneous: Write the full name of your library, including the library path. Similar to the indications on this post for building with GCC.

How to use my library in C++?

In Eclipse I have created two libraries. One of them is shared another one is static. I have compiled them in Eclipse and as a result a Debug folder was created (for both libraries) and these folders contain make-files as well as object files (*.o) and dependency reference file (*.d). In addition to that, the static library contains an *.a file.
Now I create a new project and what to use these library in this project. Normally, when I use a library I type #include <libraryname>. But if I use #include <mylibraryname> it does not work (I get unresolved inclusion). And this is not surprising because Eclipse should somehow know where my library is located. So, my question is how can I inform Eclipse about the locations of my libraries.
ADDED As recommended I do the following sequence "Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries". Then, in the "libraries(-l)" filed I add "StaticList" (because I have "libStaticList.a" file) and in the "Library search path (-L)" filed I give the full name of the directory where my "libStaticList.a" is located. Then I click Apply and OK. But it does not help. Eclipse does not like #include <StaticList>. It complains: "Unresolved Inclusion..".
#includeing headers only makes the compiler aware that the functions in those headers exist. The actual implementation of those functions needs to be linked in by the linker. That's where the library (.a) files that you built come in. Check out this thread for an example on how to link in your libraries using Eclipse.
I think you need to #include "yourlibrary.cpp" (between double quotes) instead of < >.

netbeans projects that depends on other projects and their includes

I have a certain c++ (library) project in netbeans 7.0. Lets call it the A project.
Now i have a c++ (application) project called B.
B depends on A:
1) at compile-time for some includes in A project
2) at link-time for the libA.so
2) is pretty easy, since i just need to go to project properties-> build(linker) -> libraries and click "Add Project.."
However its not that clear what the best nice, clever approach for 1) is. I've created pkg-config entries in the past to help projects find third-party libraries, but is a bit more work to do it for projects themselves. I could also include existing file directly, but i would have to add ugly ../../A/ in the includes which is PRECISELY what i'm trying to avoid
So I would like to hear about people solving this problem in the past and what was the best solution they found
You can add "an additional library directory" in the linker portion of the project properties. Then you can refer to library file "A" without the path specifier. This is a command line option to the linker to search that directory for libraries it has not found.
I'm not sure if they've added that option to the project properties for Qt projects in 7.0 or not. It's missing for those projects in 6.9.1