How do you statically link Boost libraries only?
Use the boost archive files (.a files) instead of the shared lib files (.so aka. linking with -l). You are linking those boost libraries dynamically right now. Writing it out might help to ensure what is being linked statically and what dynamically.
This will look something like:
g++ -o"acmserver" ./src/acmserver.o ... ./src/server.o \
/usr/local/lib/libboost_system.a /usr/local/lib/boost_filesystem \
... -lGL ...
Depending on the gcc version or platform type you might also have to add the -static` flag.
Linker errors when trying to statically linking Boost
Your program does not use libboost_filesystem
at all. The only
boost library it depends on is liboost_iostreams
.
Trying to statically link Boost
For pthread_mutex_init, you want to compile/link with -pthread option:
g++ -static -pthread -o"MyProgram" ./main.o -lboost_thread
The problem is that functions like pthread_mutex_init are in a separate library. Dynamic libraries can include the metadata for the fact that it needs the separate library (so libboost_thread.so includes the fact that it needs libpthread).
But static libraries don't have that information. So you need to provide the reference to any necessary libraries when you link statically.
As for using -pthread
instead of -lpthread
, it's slightly preferable because it not only links the necessary library, but provides any other options that should be used (such a -D_REENTRANT
to the compiler).
Linking statically only boost library G++
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.
Static linking with Boost Filesystem not working
You seem to be using the old CMake variables for Boost. As of Boost 1.70, Boost now provides CMake support to make it easier for CMake to find and use Boost for your project (see documentation on this page).
When you build Boost, you will see CMake package configuration files for each Boost library (typically placed along with the built libraries in a cmake
folder). You can use find_package()
in CONFIG
mode to find these package configuration files. These provide imported targets such as Boost::filesystem
that you can link to directly. This way, if the filesystem
library is missing, you don't have to sift through the list of libraries in the BOOST_LIBRARIES
variable when compilation/linking fails. Instead, CMake will tell you the library is missing at the CMake configure-time.
Also, the Boost library dependencies should now be resolved automatically. So, if you're using filesystem
, you should no longer have to explicitly call out the system
library also. Boost should resolve this dependency for you. With these changes, your find_package()
command usage could look like this:
find_package(Boost CONFIG REQUIRED filesystem)
if(Boost_FOUND)
add_executable(${PROJECT_NAME} "execute_code.cpp")
target_link_libraries(executable PRIVATE Boost::filesystem)
endif()
For what it's worth, you can run make VERBOSE=1
to see the verbose output at the link stage to verify that all the correct libraries are being linked. You should be able to see that Boost's filesystem
and system
libraries are linked, and you can verify they are the desired static (.a
) libraries.
Boost Logger Static Linking: Not Working
By default on modern UNIX-like systems gcc links with shared libraries by default. In order to force static linking you can either add -static
to your linking command line (see the docs) or make sure gcc doesn't find the shared libraries but only finds the static libraries (e.g. move the shared libraries to a separate directory while you're linking your project). Note that -static
will make all libraries linked statically, including libstdc++.
Alternatively, you can specify the static libraries directly, without the -l
switch. You will have to use the full path to the libraries though, so instead of
gcc ... -lboost_log ...
you would write
gcc ... ..../boost_1_61_0_b1/stage/lib/libboost_log.a ...
In any case, you should not define BOOST_ALL_DYN_LINK
because this macro means exactly the opposite - that you intend to link with Boost shared libraries.
Related Topics
How to Export Symbol from Linux Kernel Module in This Case
How to Assign a Name for a Screen
Linux How to Copy But Not Overwrite
How to Increase the Scrollback Buffer in a Running Screen Session
How to Extract the Contents of an Rpm
How to Call Matlab Functions from the Linux Command Line
When Setting Ifs to Split on Newlines, Why Is It Necessary to Include a Backspace
Remove Odd or Even Lines from a Text File
Linux Command History with Date and Time
Counting Number of Directories in a Specific Directory
Realuid, Saved Uid, Effective Uid. What's Going On
How to Paste Multi-Line Bash Codes into Terminal and Run It All at Once
How to Write a Linux Bash Script That Tells Me Which Computers Are on in My Lan
How to Give Arguments to Kill via Pipe
How Does Cron Internally Schedule Jobs
How to Get Ctrl-Backspace to Delete a Word in Vim Within Gnome-Terminal