errors at building a cpp code using visual studio [duplicate] - c++

I'm using the <XZY> IDE to compile my program, and have some trouble to import/integrate a specific library with it.
I get error messages like
fatal error: 3rdPartyLib.h: No such file or directory
for a
#include "3rdPartyLib.h"
statement
ld.exe: cannot find `lib3rdParty.a`
for specifying
3rdParty
in the additional libraries
At least I got some error message like
undefined reference to `lib3rdParty::foo()'
What can I do to fix this?

It's a common misconception, that the current IDE used, is responsible for getting errors like stated in the question.
See for example
c++ lib in two same project ,one can work but the other can 't
Issue linking libxml++ and glib libraries with CodeBlocks IDE for C++ Windows
...
The problem is almost never related to the currently used IDE.
In the most cases the solution boils down, to supply the actual toolchain's compiler/linker with the appropriate path's to search for included headers, to be linked libraries.
One of the major applicable answers for the linker related problems are
What is an undefined reference/unresolved external symbol error and how do I fix it?
undefined reference to `WinMain#16'
Most of the common IDEs provide features to configure this for a specific project. Here are some samples
Eclipse-CDT
Include path settings:
Library & library search path settings
Visual Studio 2013
Code Blocks
**DEV C++ (Bloodshed C++)
As from their FAQ:
9. How can i use the OpenGL library and others ?
All the libraries that comes with Mingw reside in the Lib directory. They are all named in the following way: lib*.a
To link a library with your project, just add in Project options, Further option files :
-lopengl32
This is for including the libopengl32.a library. To add any other library, just follow the same syntax:
Type -l (L in lowercase) plus the base name of the library (filename without lib and the .a extension).
You may also consider putting a -L option there to add directory pathes searched for libraries.
Qt Creator
In order to add include paths you have to open up the .pro file and then add
the directories to the INCLUDEPATH variable. These paths are separated by spaces. Reference can be found here.
If none of the above samples applies for your actually used IDE/toolchain, I hope you're able to get the point of abstraction:
It's an issue how to provide compiling/linking options to your actual toolchain. The IDE used to setup the context is a minor point here.

For eclipse I use the pkg-config plugin where possible:
https://marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
Failing that this configuration window:

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

Import files exists to project eclipse C++ Linux

I am using eclipse to dev my application for linux. I have my library source in workspace/mylib/ and the project in workspace/project.
In a source of my project i include the relative path of source library file,
example: #include "../mylib/foo.h"
The problem start when i try build it, in all parts that use library source eclipse shows this error: undefined reference to 'function()'
I think that it is by library files don't have in src folder or not link.
Any Idea?
Thanks!
error: undefined reference to 'function()'
Indicates that the linker cannot find the definition of the function(). Either you have not defined it in your source code Or If you are using a library which defines it, the library is not getting linked.
This or this posts explain what you should do to add external libraries to eclipse.

Error linking libboost_system in Visual C++

I have a problem using boost.asio. I use Windows XP and Visual Studio 2005
I followed these steps:
I downloaded the latest version (http://sourceforge.net/projects/boost/files/boost/1.46.1/) of W: \boost_1_46_1
I set the environment variable INCLUDE : W:\boost_1_46_1
In the properties of my project (VS->tools->Options project&solutions->vcc++directories) I added $(INCLUDE)
I added in my file. h this:
I get the following error:
LINK : fatal error LNK1104: cannot open file 'libboost_system-vc80-mt-gd-1_46_1.lib
This is a linker error. Find the lib, and go to linker input, and add it there. Be sure to set the libpath as well. This picture should guide you:
The boost::asio library needs to be built. ( Many other boost 'libraries are headers only and do not need to be built ). Building it yourself is a small challenge. Better to use the pre-built binary installer which is here
Once you have the built libraries, one way or another, you have to tell the linker where they are. However, you do not need to specify each individual library by name, just specify the folder that contains them. The easiest way to do this IMHO is to create an environment variable called BOOSTROOT which you you set equal to the folder where you installed boost. Then you tell the linker to look for libraries in $(BOOSTROOT)/lib

Why is VisualStudio looking for this lib file? LNK1104 error

We have a large project using VS2008 and boost 1_42. I'm trying to upgrade to VS2010 and boost 1_44. I installed VS2010 and boost 1_44 and converted the project. Now I am trying to build, and everything compiles, but fails when linking:
LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-1_42.lib'
I have changed the include and lib directories to point to the new boost 1_44 files and I have renamed the old boost 1_42 directory.
Why is the linker still looking for a vc90-1_42 file, when it is only using 1_44 headers? Is there a way that I can determine WHY the linker wants this file? The linker obviously thinks it needs the file, but why?
I have cleaned the project and I am re-building to ensure any old build files are erased.
I've run into exactly this problem a couple of times too. It's usually been some old temporary files but like in your case cleaning didn't always do the trick straight away. Does your project include any static libs that might have been built with 1.42?
Something you can try which may or may not be helpful in tracking down your issue:
Rename the old boost directory back to it's original name
Clean the solution
Under C/C++->Command Line->Additional Options add "/showIncludes"
Under Linker->Command Line->Additional Options add "/verbose:lib"
Rebuild all
Then when you build you'll be able to see at which point 1.42 headers are included, etc. in the output window. Somehow doing this helped me in tracking down where the problem was.
Along with changing the lib directory, you need to change the name of the boost library. That's in the Linker | Input section of the project settings.
Your added comment makes it clear that the dependency on the Boost 1.42 library was being created indirectly by another library that hadn't been rebuilt.
For this you basically have two choices: either add that library as a project to your main solution, and make sure it has enough dependency information that it'll be re-built when you upgrade Boost, or use the /Zl compiler switch when you build your library. This tells the compiler you're building a library so you do not want to embed library dependencies like this.
Boost uses
#pragma comment(lib)
command to inform the linker of libraries it needs to link with. It is not an error. If Boost says you need it, it's likely you do.
On How can I find out why the linker wants this file?
There are programs which will go through your app and dlls/libs and report the content of manifests and what the binaries report they depend on. You could then scan the report for the unexpected libraries being included. We used this mainly to find libs including the previous version of the VC runtime.
Have not used the one we had in about 5 years though, now if only I could remember the name of the app!
DependancyWalker (depends.exe) will allow you to see dependancies of dll/exe but not static libs.
You could open each binary as a 'file' in MSVS and look at the manifest content by hand, but I imaging this would be a bit painful. I've not tried this with a static lib.

C++ linker options for DLL (DEF, LIB, etc)

I have downloaded a library for the purposes of writing a program that can uncompress a RAR file. (http://www.rarlab.com/rar/UnRARDLL.exe) This supplies me with unrar.dll, unrar.h, unrar.lib and UnRDLL.def. I have copied the C example code and have tried compiling it with both Dev-Cpp and Eclipse.
I don't have much experience using DLLs, so I don't know how to deal with the following linker errors:
UnRDLL.o(.text+0x151):UnRDLL.c: undefined reference to RAROpenArchiveEx#4'
UnRDLL.o(.text+0x1c0):UnRDLL.c: undefined reference toRARSetCallback#12'
UnRDLL.o(.text+0x1e2):UnRDLL.c: undefined reference to RARReadHeader#8'
UnRDLL.o(.text+0x2b9):UnRDLL.c: undefined reference toRARProcessFile#16'
UnRDLL.o(.text+0x2fe):UnRDLL.c: undefined reference to RARCloseArchive#4'
UnRDLL.o(.text+0x366):UnRDLL.c: undefined reference toRAROpenArchiveEx#4'
UnRDLL.o(.text+0x3d6):UnRDLL.c: undefined reference to RARSetCallback#12'
UnRDLL.o(.text+0x41c):UnRDLL.c: undefined reference toRARReadHeaderEx#8'
UnRDLL.o(.text+0x4c2):UnRDLL.c: undefined reference to RARProcessFile#16'
UnRDLL.o(.text+0x4fa):UnRDLL.c: undefined reference toRARCloseArchive#4'
Google suggested adding --def UnRDLL.def and -lunrar to the linker options and also copying the .lib file to the Dev-Cpp\lib directory.
Can you please explain to me what I'm doing wrong? If possible, tell me what files need to be in the source code directory, what needs to be with libraries, what needs to be added to the project and what linker options there need to be, as well as anything else I've totally missed.
EDIT: I don't know why, but I just manually redid all the settings as described above and now it works. Thanks for your help anyway.
I recommend using Visual Studio Express (freely available from Microsoft at the provided link) to compile and link your program. I think it's a bit simpler in this instance than the other tools that you mentioned, although that's just my personal opinion.
I recommend using a layout similar to this for your project:
\myproject
\src
\include
\thirdparty
\bin
\lib
\include
Your C/C++ source files will live under myproject\src and your header files will live under myproject\include. The library files that you downloaded live under thirdparty: the DLL belongs in bin, the .lib file and .def file belong in lib, and the library's header files belong in include.
Next, you need to configure your project in Visual Studio Express. In your project properties, under Linker -> General, add \thirdparty\lib to Additional Library Directories. Under Linker -> Input, add unrar.lib to Additional Dependencies. This tells Visual Studio Express the name and location of your thirdparty library, so that it can link it into your main application.
When running your program, you'll need to copy unrar.dll to your project's output directory so your program can load it.
This should help you to get going...
Back in the day, Borland used to supply a tool for creating a lib file from a def file.
In the modern world, your best bet is to add the lib file to your project under Project->Properties Config Props->Linker->Input->Additional Dependancies.
You may need to add the lib file location to Props->Linker->General->Additional Lib Dirs.
Make sure the DLL is on your path or copied in with the executables.