Simple Boost code doesn't build - c++

When I am including the asio header:
#include <boost/asio.hpp>
I got the error:
undefined reference to boost::system::generic_category()
So I read that I had to link to boost_system which I did. But now it gives the error::
cannot find -lboost_system
Why can it not find boost_system. I'm using Codeblocks on Windows.

boost is a separate library that needs to be installed on you building machine.
Please follow these instructions to install:
http://www.boost.org/doc/libs/1_55_0/doc/html/bbv2/installation.html
http://www.boost.org/
After installation your application needs to be able to find it if it is not installed in a standard location.
you will need to supply the header and lib directories to the build process.
g++ source source.cpp -I/path to boost headers -L path to boost libs -lboost_system
Look inside you libs directory to be sure that you have boost_system. It is possible depending on the build that you may only have boost_system-mt

Related

Linking against Boost on Ubuntu x64

After reading a lot of SO questions on the matter, I just couldn't get it to work. I downloaded boost_1_60_0, then I ran the commands to build it:
sudo ./bootstrp.sh --prefix=/home/ricardo/boostlib
sudo ./b2 install -j8
I even tried running b2 like this:
sudo ./b2 install -j8 architecture=x86 address-model=64
Does not matter. The error is always the same:
main.cpp:(.text+0x7e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x8a): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x96): undefined reference to `boost::system::system_category()'
Yeah, I know. Linking error, should run with -lboost_system and all. Yeah, you should put -L/home/ricardo/boostlib/lib. I know.
This is what my CMake looks like:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system -Wl,--verbose")
include_directories("/home/ricardo/boostlib/include")
You might be asking: What's the output of -Wl,--verbose? Well, surprisingly enough, libboost_system.so has been found:
attempt to open /home/ricardo/boostlib/lib/libboost_system.so succeeded
-lboost_system (/home/ricardo/boostlib/lib/libboost_system.so)
Okay. The library was found. I'm still trying to find some alternatives, nothing seems to work so far. So, even though I'm linking it against boost, and boost_system has been found, the program still does not compile due to... why? Can someone explain me?
Edit: How to link C++ program with Boost using CMake seems to be working. Though I still would like to know why my method does not work, and what should I do when I want to link against boost using just the g++ compiler, without CMake and Make. I've been able to link against Openblas succesfully before, so I wonder why it isn't working with Boost.
Edit2: This is the g++ command I got after running make VERBOSE=1, now I can see that the sources are being put AFTER the dependencies.
-std=c++11 -L/home/ricardo/boostlib/lib -lboost_system CMakeFiles/prophet-service.dir/main.cpp.o -o prophet-service -rdynamic
And this is my current CMake file:
cmake_minimum_required(VERSION 3.2)
project(prophet-service)
set(SOURCE_FILES
main.cpp)
include_directories("/home/ricardo/boostlib/include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/home/ricardo/boostlib/lib -lboost_system")
add_executable(prophet-service ${SOURCE_FILES})
It also seems that I don't have the libbost_system.a file. At least now I know that I need the .a file instead of the .so file.
Are you linking in the right order? The thing that HAS the dependency needs to go before the thing that SATISFIES the dependency.
You need to link version of the library with extension .a because linker requires this extension (used for static libraries) and not .so which is used for shared libraries (the same of dll for Windows).
For example, if I search for libboost_system.a on my Ubuntu operating system I find the following:
frar#Home-PC:~$ locate libboost_system.a
/home/frar/Documents/SVILUPPO/boost_1_59_0/bin.v2/libs/system/build/gcc-4.8/release/link-static/threading-multi/libboost_system.a
/home/frar/Documents/SVILUPPO/boost_1_59_0/stage/lib/libboost_system.a
/usr/lib/x86_64-linux-gnu/libboost_system.a

Boost.Log linking errors under GNU/Linux

I would like to test the Boost.Log library. Unfortunately, I get link errors.
I use Arch Linux, and I installed Boost headers and libraries via built-in package manager pacman:
boost 1.54.0-3
boost-libs 1.54.0-2
When compiling the simple example from official site via g++ log.cpp -lboost_log -lpthread, I get the following errors:
log.cpp:(.text+0x42): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
log.cpp:(.text+0x9b): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
...
I've read Why my application fails to link with Boost.Log?, but I couldn't solve the link errors. It only gives me the hint that the library where boost::log::v2s_mt_posix::trivial::logger::get() is in was linked statically. But under directory /usr/lib/ there are only dynamically linked Boost libraries with extension .so.
Maybe, someone has a clue what's going wrong here.
Thank you.
You need to define BOOST_LOG_DYN_LINK:
g++ -DBOOST_LOG_DYN_LINK blog.cpp -lboost_log -lpthread
If you are using cmake then:
find_package(Boost REQUIRED COMPONENTS system log)
target_link_libraries(credential ${Boost_SYSTEM_LIBRARY} ${Boost_LOG_LIBRARY})
and use:
#define BOOST_LOG_DYN_LINK 1

Problems cross-compiling a Matlab oct-file using Mingw64 on Ubuntu

I wrote a C++ application inclunding the external libraries glpk and boost with a binding to the Matlab API. On Ubuntu compiling in Matlab using mex works fine, the program is running without difficulties.
Now to use the same application on a Windows Platform I wanted to cross-compile it, using Mingw64. Corresponding to this thread it can be done with the following command:
x86_64-w64-mingw32-gcc -m64 -shared -I"/usr/local/MATLAB/R2011a/extern/include" -I"/usr/local/include" -I"/usr/include" -DMATLAB_MEX_FILE -o output.mexw64 input.cpp -L"/usr/local/MATLAB/R2011a/bin/glnxa64/" -lmex -lmx -lmat -leng -L"/usr/lib" -lglpk
Path /usr/local/include contains the header files for the boost library (header only library)
Path /usr/include contains the header files for the glpk library
Path /usr/lib contains the source files for the glpk library
But running this command gives me the error that there are conflicting declarations of the type:
/usr/include/sys/types.h:110:19: error: conflicting declaration ‘typedef __ssize_t ssize_t’
/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/include/_mingw.h:394:35: error: ‘ssize_t’ has a previous declaration as ‘typedef long long int ssize_t’
It seems that the path /usr/include causes problem for the compiler. But it is needed to include the glpk header files. Any ideas to solve the problem?
If your glpk headers are in /usr/include, chances are you didn't compile the library for Windows. You should cross-compile all libraries for the target platform (and install them in /usr/x86_64-w64-mingw32 (if I deduce your installation details correctly).
One big gotcha you're not going to be able to solve: your Matlab is Linux, and doesn't contain any Windows linkable libraries, so you're out of luck anyways.

Boost installation and library paths

I have been using Boost (the header only library part) for sometime now. I recently started on a project that required the compiled libraries (filesystem etc).
I followed the instructions given in the documentation, and was under the impression that the libraries to installed (directly) in the usr/local folder. After a lot of trial and error, I found that the correct (*.a) files to use were in:
/usr/local/boost_1_45_0/stage/lib/
Is this the correct folder to use for linking the boost built shlibs (shared libraries)?
An example for linking regex static(*.a) lib:
g++ -I /usr/local/boost_1_45_0 -c your_regex_prog
g++ -static -o static_regex your_regex_prog.o -lboost_regex

Adding Boost Library to a C++ project in OS X Eclipse

I am have been attempting to get a C++ project setup using boost file system library using eclipse. I followed these directions to install boost on my system. The directions where pretty much
download
extract
run bootstrap.sh
run ./bjam architecture=combined
That seemed to go fine, no errors. I then fired up eclipse and created a new test project called test with a single file called test.cpp. The code in it is:
#include <stdio.h>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path path("/Users/schoen"); // random pathname
bool result = boost::filesystem::is_directory(path);
printf("Path is a directory : %d\n", result);
return 0;
}
This is just something simple to make sure it is all set up correctly. Of course I tried to compile at this point and it failed. Did some googling and found this site. It said to add the boost library to the linker by going to project properties and adding "boost_filesystem". I tried this, and well it didn't work.
Can someone point me in the right direction or give me a hint to how to set up Boost in an Eclipse project?
I am new to C++ and Eclipse, and most my experience is in Java with Netbeans. So I am pretty lost at the moment.
UPDATE
I just wanted to update on what I have tried based on the answers given.
Based on Alex's suggestion I added boost_system and boost_filesystem to the linker list. I was still getting the same compiler errors.
Following the suggestion from rve I added the path to the boost libraries to the Library search path. When this did not work. I cleared out the linker list and tried it with just the library search path. This also did not work.
I then cleared the Library search path. I then manually edited the command on the linker window to be 'g++ -L/Users/jacobschoen/Library/boost_1_45_0/stage/lib -lboost -lboost_filesystem'. This also did not work.
In all of these I tried setting the path to boost to be '/Users/jacobschoen/Library/boost_1_45_0' and '/Users/jacobschoen/Library/boost_1_45_0/stage/lib'. Neither worked.
As requested the comiler error for the above code is:
**** Build of configuration Debug for project test ****
make all
Building file: ../src/test.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.d" -o"src/test.o" "../src/test.cpp"
../src/test.cpp:10:32: warning: boost/filesystem.hpp: No such file or directory
../src/test.cpp: In function 'int main()':
../src/test.cpp:13: error: 'boost' has not been declared
../src/test.cpp:13: error: expected `;' before 'path'
../src/test.cpp:14: error: 'boost' has not been declared
../src/test.cpp:14: error: 'path' was not declared in this scope
make: *** [src/test.o] Error 1
If any one has any further suggestions I am still trying.
Second Update
On a suggestion by rholmes I added an include library along with the linker list and library search path. So now the compile error is:
**** Build of configuration Debug for project test ****
make all
Building target: test
Invoking: MacOS X C++ Linker
g++ -L/Users/jacobschoen/Library/boost_1_45_0 -o "test" ./src/test.o -lboost_system -lboost_filesystem
ld: library not found for -lboost_system
collect2: ld returned 1 exit status
make: *** [test] Error 1
Any ideas?
Just wanted to be clear on what actually worked, since it was kinda pieced together from a few answers.
Download the boost files and extract them to where you want to put them.
In your terminal navigate to the directory and run ./bootstrap.sh
When that is done run ./bjam (this takes a while so go smoke and get a cup of coffee)
Open up your eclipse Project and go to Project > Properties > C/C++ Build > Settings
Click on MacOS X C++ Linker > Libraries.
You should see a split window with the top being for 'Libraries (-l)'. In this section add both boost_system and boost_filesystem. In the bottom section it should be for 'Library Search Path (-L)'. Here you want to put the path to the stage/lib directory inside where you extracted the boost download. It should look similar to below:
Click GCC C++ Compiler > Includes. This will be a single pane where it says 'Include Paths (-I)', well I think it is an I as he font is weird and could be a lower case l also. Anyway in that section add the path to where you put boost without the stage/lib part. It should look like below:
Everything should compile now with out a problem, and if you need to use any other boost libraries it should be just a matter of adding it to the linker section where boost_filesystem and boost_system are. Enjoy.
Not sure where you do this in Eclipse these days, but under the include paths for Eclipse should be the path to the main boost directory (/Users/jacobschoen/Library/boost_1_45_0?). The compiler line should have something like the following in it, I would think:
Invoking: GCC C++ Compiler
g++ -I/Users/jacobschoen/Library/boost_1_45_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD (etc..)
Update: Looking at my system, the linker path on yours might be more appropriately:
-I/Users/jacobschoen/Library/boost_1_45_0/stage/lib
Depending, of course, upon how you've installed and built boost -- this is with my most recent attempt with a full source build. Depending upon how you obtained boost, this may or may not be different. I recently redid the boost on my Mac for 64 bit and haven't had much time to try it yet....
Add boost_system to the linker list, together with boost_filesystem.
I had recently uninstalled the boost rpm and installed Boost like how you did. I had no problems running Boost programs in Eclipse. I didn't add any extra parameters. Just installed boost and ran Boost programs. It works fine.
Tried your program in the vi editor. Commented out everything in main
#include <cstdio>
#include <boost/filesystem.hpp>
int main() {
/*boost::filesystem::path path("/Users/schoen"); // random pathname
bool result = boost::filesystem::is_directory(path);
printf("Path is a directory : %d\n", result);*/
return 0;
}
and it still gave this error:
/tmp/cc7TAIYS.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x29): undefined reference to `boost::system::get_system_category()'
test.cpp:(.text+0x35): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x41): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x4d): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x59): undefined reference to `boost::system::get_system_category()'
collect2: ld returned 1 exit status
I'm puzzled. Boost programs work on my system, but your program's header files itself are giving a problem. I doubt it's a problem with Eclipse. It has to be something else.
I just ran into something very similar to this using eclipse and CDT... It turns out, using ubuntu and apt-get, libboost_system installs as libboost_system.1.40.0 in /usr/lib
If you try to add it via the library tab in Helios it will complain because it is looking for *.so and *.s0.1.40.0 clearly doesn't match that. However after looking closely at what the linker was trying to doo, I just typed the raw string "boost_system" into the include path adder. This resulted in the linker doing a " -lboost_system" which is a format the linker knows how to deal with in resolving version dependency... If you instead put in the full path to the .so file, the linker will just complain because it tries to do a " -l/usr/lib/libboost_system.so.1.40.0" .
So take my advice and just type in the simple " boost_system" after doing an apt-get install.. It will make it all very easy.