Setting up a library in Eclipse CDT - c++

I am creating a project which needs to use the libnoise headers. However I am having difficulty compiling.
I have put the headers into my src/ directory of my project, so I am able to include them with
#include "noise/noise.h"
#include "noise/noisegen.h"
#include "noiseutils.h"
But when I try and create a Perlin module noise::module::Perlin perlinModule;, I get the following compilation error:
PROJECTDIR\bin/../src/libnoise_wrapper.cpp:66: undefined reference to `noise::module::Perlin::Perlin()'
./src/libnoise_wrapper.o: In function `~Perlin':
PROJECTDIR\bin/../src/noise/module/perlin.h:160: undefined reference to `vtable for noise::module::Perlin'
PROJECTDIR\bin/../src/noise/module/perlin.h:160: undefined reference to `noise::module::Module::~Module()'
PROJECTDIR\bin/../src/noise/module/perlin.h:160: undefined reference to `vtable for noise::module::Perlin'
PROJECTDIR\bin/../src/noise/module/perlin.h:160: undefined reference to `noise::module::Module::~Module()
I have created a Project Include reference to the /src/noise folder as well as noiseutils.h file (which is in the src/ directory itself).
Are there any other configurations I am missing?

"I have put the headers into my src/ directory of my project, so I am able to include them with "
That's not the usual way to do! You should have an installation of this library in your environment (there are some instructions how to do this for your particular case available in this tutorial from the libnoise online documentation), and add the additional include paths to search using the
Project Properties->C/C++ Build->Settings-><actual C++ Compiler>->Includes->Include paths (-I)
settings.
"Are there any other configurations I am missing?"
Check the
Project Properties->C/C++ Build->Settings-><actual toolchain linker>->Libraries
properties page.
You'll need to provide noise in the Libraries list there, and eventually an additional Library search path pointing to the actual libs installation. The latter depends on how and where you have the libnoise installed in your environment.

Related

Including STXXL library in the MakeFile

I was trying to install STXXL library to a custom path following this answer supplying prefix to cmake this way:
cmake -DCMAKE_INSTALL_PREFIX=/usr . && make all install
When I run the tests, they seems to be working fine. But I want to include STXXL in a different MakeFile and compile that project. In that MakeFile there a line
STXXL_CONFIG = /opt/stxxl/stxxl.mk
I believe the configuration file stxxl.mk comes from the old make based installation (I couldn't locate it in my system either). I was wondering how I can modify this file to include STXXL library and get the custom project compiled.
Without modifying the above statement in MakeFile, I am getting the error:
undefined reference to 'stxxl::get_next_seed()' collect2: error: ld returned 1 exit status
It goes without saying that I don't have root access and unfortunately, neither a good background with MakeFiles. This is not a duplicate of Makefile with STXXL
To use a third party C++ library from a non-standard location in GNU Make the regular steps are:
Add the path to 3rd-party library headers to your C++ preprocessor flags. E.g.
CPPFLAGS += -I/3rd-party/include
Add the path to 3rd-party shared/staric library to your linker flags and the library itself. Assuming the library is named lib3rd-party.so or lib3rd-party.a, e.g.
LDFLAGS += -L/3rd-party/lib -Wl,-rpath=/3rd-party/lib -l3d-party

How to add FLTK lib into netbeans project

I'm studying Stroustrup Programming principles and practice and have a problem with using FLTK library with netbeans
What I've done:
downloaded library
In netbeans: New project with existing sources (used path where I saved this lib), compiled it without errors.
In my test project: Properties->C compiler->Include directories added path to library, the same for C++ compiler
Properties->Linker->Additional Library Directories added path to .a files
Libraries->Add Library added all 7 libraries from fltk/lib/*.a
Now when I try to compile my project I get a pile of errors like that:
../../../workspace/Study/fltk-1.3.4-2/lib/libfltk.a(screen_xywh.o): In function `screen_cb':
c:...\fltk-1.3.4-2\src/screen_xywh.cxx:72: undefined reference to `CreateDCA#16'
c:...\fltk-1.3.4-2\src/screen_xywh.cxx:74: undefined reference to `GetDeviceCaps#8'
c:...\fltk-1.3.4-2\src/screen_xywh.cxx:75: undefined reference to `GetDeviceCaps#8'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/studystr.exe] Error 1
I have no idea how to fix that(((
I've found solution to this problem: adding './fltk-config --compile' to Linker additional options in project's properties helped.
Also this article was usefull: http://www.fltk.org/articles.php?L598+I140+T+P1+Q

Eclipse setting for including paths

I am trying to compile the Dlib library in Eclipse, but have an error in linking.
According to: http://dlib.net/compile.html I have to include the path containing the dlib folder (that's what I did) and include the source file in my project: dlib/all/source.cpp.
I keep on getting the following error:
../source.cpp:7:41: fatal error: ../base64/base64_kernel_1.cpp: No such file or directory
This is a line from the source.cpp file. The directory looks like:
/usr/include/dlib-18.6/dlib/base64, If I add that path in my library I get the next error:
In function dlib::threads_kernel_shared_helpers::thread_starter(void*)':
/usr/include/dlib-18.6/dlib/base64/../threads/threads_kernel_2.cpp:37: undefined reference topthread_detach'
Do I have to keep adding paths after each error?
Why doesn't Eclipse just add all subpaths of my /usr/include/dlib-18.6/ (that's the path containing dlib and the it's the path I added)?
I think it depends a bit, on how you had setup your particular toolchain, to build your main/dlib project.
Building using e.g. GCC 4.8 (and using the -std=c++11 option) might require to specify the -pthread option on linking stage, other environments might want to link against -lpthread.

C++ include libraries

Ok, so it's been a while, and i'm having problems with #includes
So I'm doing
#include "someheader.h"
but it's giving me
fatal error: someheader.h: No such file or directory
It's a system wide library I guess you could say.
I'm running arch linux and I installed the library from the repo, and I think the .h files are in /usr/include.
I could just copy all the header files into the folder my code is in but that would be a hack.
What is the "right" way to do this?
Edit: I wasn't correct by saying the .h files were in /usr/include, what I meant was that the library folder was in there
So, Emile Cormier's answer worked to a certain extent.
The problem now is that there are some include in the header file and it seems from the methods I'm trying to access that those includes are not happening
it's giving my the error
undefined reference to Namespace::Class::method()
Edit:
Ok so the final answer is:
#include <library_name/someheader.h>
And compile with
g++ code.cpp -llibrary_name
Sometimes, header files for a library are installed in /usr/include/library_name, so you have to include like this:
#include <library_name/someheader.h>
Use your file manager (or console commands) to locate the header file on your system and see if you should prefix the header's filename with a directory name.
The undefined reference error you're getting is a linker error. You're getting this error because you're not linking in libsynaptics along with your program, thus the linker cannot find the "implementation" of the libsynaptics functions you're using.
If you're compiling from the command-line with GCC, you must add the -lsynaptics option to link in the libsynaptics library. If you're using an IDE, you must find the place where you can specify libraries to link to and add synaptics. If you're using a makefile, you have to modify your list of linker flags so that it adds -lsynaptics.
Also the -L <path_to_library> flag for the search path needs to be added, so the linker can find the library, unless it's installed in one of the standard linker search paths.
See this tutorial on linking to libraries with GCC.
You'd use #include <someheader.h> for header files in system locations.
#include "someheader.h" would try to include the file someheader.h in the directory of your .c file.
In addition to including the header file, you also need to link in the library, which is done with the -l argument:
g++ -Wall youprogram.cpp -lname_of_library
Not doing so is the reason for the "undefined reference .. " linker errors.
The quick fix is to do use:
#include <someheader.h>
assuming that someheader.h is in the standard include locations (to find it use the command locate someheader.h in a shell. If it is in /usr/include it is in a standard location. If it is in a subdirectory of /usr/include you only need to add the part of the directory up to /usr/include in the #include directive (e.g. #include <fancy_lib/someheader.h>)
However, this is only half of the story. You also will need to set up your build system in a way that locates the given library and adds its include path (the path under which it's header files are stored) to the compiler command (for gcc that is -I/path/to/header). That way you can also build with different versions by configuring them in your build system. If the library is not header-only you will also have to add it to the linker dependencies. How this is achieved in your build system is best found out by consulting its documentation.

C/C++ ftplib: Undefined reference to _imp__FtpInit

I am trying to use a C/C++ FTP library (>here's the website). When I try to call the FtpInit() function I get a compilation error telling me that there is an undefined reference to _imp__FtpInit. This is what I'm trying to compile:
#include "ftplib/ftplib.h"
int main()
{
FtpInit();
return 0;
}
Did you remember to set your -L library path to the location where you installed the library, and use -lftplib (or similar depending on the actual library file's name) to link the library?
For Visual Studio, add the .lib file to the "Additional Dependencies" setting in project properties/Linker/Input.
You may also need to set "Additional Library Directories" in Project Properties/Linker/General