I am in the process of making a basic role-playing game. I want to include the Boost libraries statically so that the people who run my game do not need to have them. I researched and looked-up that all you have to do is add -static to the command-line compile, so my command is like this:
$ g++ -static -o karthas *.o -lncurses -lmenu -lboost_system -lboost_filesystem
But apparently the -static is affecting ncurses. I am getting a whole bunch of errors, most of which are undefined reference to 'SP'.
Is it possible to just do a static link to Boost and not ncurses? How would I go about doing that?
You can choose which libraries will be linked statically and which will be linked dynamically by putting either -Wl,-static or -Wl,-Bdynamic before their name.
For example, with:
g++ -o karthas *.o -Wl,-static -lmenu -lboost_system -lboost_filesystem -Wl,-Bdynamic -lncurses
The menu, boost_system and boost_filesystem libraries will be linked statically and ncurses dynamically.
(But you can also distribute the boost dlls with your executable, and not link anything statically).
But looking at this, it seems that you are not alone, either that, or I found your issue. But this, might have your solution, either way, good luck.
Btw, some boost libraries are little more than inline functions that are imported when included in the file.
Related
I found a lot of similar problems, but I was not able to apply any given solution for me. A few months ago, I worked on a project using the boost library. I compiled simply in the command line, as described on the boost website.
g++ -I pathToBoost file.cpp -o file -lboost_system -lboost_filesystem
The two required linkings to boost_system and boost_filesystem were also done. This worked perfectly, but now suddenly an error occurs when i tried to compile it again.
/usr/bin/ld: /tmp/ccM2BzEo.o: in function `boost::system::generic_category()':
file.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x7):
undefined reference to `boost::system::detail::generic_category_instance'
Well, it seems to me that there is an error linking the boost_system library. Since the same thing worked before, is a problem with the compiler possible?
I used
Boost 1.68.0
g++ (GCC) 8.2.1
ManjaroLinux 18.0.3
I hope that somebody can help me here and that I was not just too stupid to see a solution in another thread.
Greetings!
The order of libraries in linker command line is important. boost_filesystem depends on boost_system, hence the fix:
-lboost_filesystem -lboost_system
Alternatively, you can surround the list of libraries with --start-group and --end-group to make the linker re-scan the libraries multiple times until it either fails or resolves all symbols, so that no specific ordering of libraries in the command line is necessary. However, it may take longer to link. E.g.:
g++ -I pathToBoost file.cpp -o file -Wl,--start-group -lboost_system -lboost_filesystem -Wl,--end-group
Since you specify -I pathToBoost you also need to specify -L<path-to-boost-libs> -Wl,-rpath=<path-to-boost-libs>. Otherwise it uses headers from one version of boost from pathToBoost, but links wrong libraries from your system boost.
I've been doing a simple c++ program with use of curlpp library. I can build and compile everything just right and I can run it if having MinGW in my PATH. When I delete it and copy all curlpp dlls in the directory, it says (of course):
The program can't start because libgcc_s_dw2-1.dll is missing from your computer. Try reinstalling the program to fix this problem.
I believe that the problem is that std libraries are not linked statically, but I tried all possibilities and it seems as they are not linked. I type:
g++ -o myApp.exe main.cpp -std=gnu++11 -L(curl,curlpp,opensll) -I(all includes) -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic -lcurlpp -lcurl -Wl,--as-needed
with no errors, then I run it and I get error from above. Of course, if I have MinGW in my PATH, then everything works.
I tried to change the order of options in the command without any luck. Putting -static-libgcc -static-libstdc++ as advised elsewhere is not working.
Thanks in advance
Regards
I have a makefile project in which I include a few different libraries. One of them is the boost library which I statically link in order to make my program portable. This is how my makefile command looks like:
g++ -O0 -g test.cpp testObject.o -pthread -I/home/user/devel/lmx-sdk-4.7.1/include/ -L/home/user/devel/lmx-sdk-4.7.1/linux_x64 -llmxclient -lrt -ldl -lboost_filesystem -lboost_system -static -static-libgcc -o $#
I have also linked lmx-sdk library to my project in order to use the licensing functionality; however, it seems to be that lmx-sdk doesn't seem to like static link as it gives an error "Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking".
How can I make it possible to link some libraries statically and the other ones dynamically ?
Thanks in advance
P.S. I have checked some of similar topics and tried a few methods which didn't work out for me.
Using -Wl,-Bdynamic and -Wl,-Bstatic instead of just using -Bdynamic and -Bstatic solved the problem.
The full link line looks like this now:
g++ -O0 -g test.cpp testObject.o -pthread -Bdynamic -I/home/user/devel/lmx-sdk-4.7.1/include/ -L/home/user/devel/lmx-sdk-4.7.1/linux_x64 -llmxclient -lrt -ldl -Wl,-Bstatic -lboost_filesystem -lboost_system -o $#
You can use -Bstatic to statically link what comes after it, then -Bdynamic to do the opposite. As many times as you need on the command line.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Can I mix static and shared-object libraries when linking?
I want to compile my app, linking statically only boost_system library. Other(glibc and etc) should be linked dynamically. How can i do it?
My command to compile dynamically:
g++ -o newserver server.cpp ... -lboost_system -std=c++0x
Command to compile statically:
g++ -static -o newserver server.cpp ... -pthread -lboost_system -std=c++0x
But this command link statically all! And app weights 2mb more!
Can you advice me what command to compile statically only boost lib?
Thanks!
Replace -lboost_system with -Wl,-Bstatic -lboost_system -Wl,-Bdynamic. The -Wl option sends the thing after it to the linker, in the order it appears on the command-line.
There are two solutions. You can specify -Bstatic and
-Bdynamic in the command line, each affects all of the
libraries which follow it. Or you can arrange it that the
static versions of the libraries which you want to be linked
statically are present in a directory which is searched before
the directory which contains the dynamic version. This allows
you to make some sort of global decision: you create the
directory once, and all users you do a -L for it before the
-L general will use the static versions.
In practice, I can't think of a case where you'ld want to link
the Boost libraries other than statically, so the simplest
solution might just be to remove the .so files. The only time
g++ will make a decision (and take into account the -Bstatic
and -Bdynamic) is if it finds both in the same directory. It
searches the directories in the given order, and when it finds
a directory which has either the static or the dynamic version
of the library, it stops. And if only one version is present,
it uses that one, regardless.
I don't want to have the end use have to have the libraries installed, so, having the libraries packaged in my exec would be preferred.
this is the relevant line in the make file:
hPif : src/main.o src/fann_utils.o src/hashes.o src/Config.o
g++ -o hPif src/main.o src/fann_utils.o src/hashes.o src/Config.o -static -lfann -lboost -L/usr/local/lib
I'm trying to link fann and boost, and I read somewhere (http://www.adp-gmbh.ch/cpp/gcc/create_lib.html) that using the -static flag allows that.
What am I doing wrong?
The -static flag is correct, but you need to make sure your libraries are static libraries without dependencies. If they are built as shared (or have shared dependencies), gcc will not link them statically (and/or you will still have library dependencies).
You may need to rebuild your Boost libraries to achieve this.