I have application built in Ubuntu 14.04 / GCC 4.8 / CMake 2.8 with -static-libgcc and -static-libstdc++. ldd command shows:
linux-vdso.so.1 => (0x00007ffc16195000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcb18256000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcb17f50000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcb17b8a000)
/lib64/ld-linux-x86-64.so.2 (0x000055fc367e1000)
When I add -static to CMAKE_EXE_LINKER_FLAGS - I got different output from ldd:
linux-vdso.so.1 => (0x00007ffd48349000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe8d0335000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe8d0031000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe8cfd2a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe8cf965000)
/lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x000055d61ec7d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe8cf74f000)
I supposed -static flag will result in static linked application. It did not happen.
May anyone explain me why ?
I have makefile generated by Qt Creator - with -static switch build results in fully static application.
-static is different from -static-libgcc and -static-libstdc++ specifically in that -static does not assume static linking of standard libraries. Those two flags control this behavior.
To link statically with standard libraries, add those two flags to CMake CMAKE_EXE_LINKER_FLAGS variable.
It is from resulting CMakeLists.txt. I use -DSTATIC_LINKING=True when running CMake.
IF(STATIC_LINKING)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
ENDIF(STATIC_LINKING)
# libpcap library
find_library(pcap libpcap.a)
# opencore-amr libraries
find_library(opencore-amrnb libopencore-amrnb.a)
find_library(opencore-amrwb libopencore-amrwb.a)
# c++ netlib
find_library(cppnetlib-server-parsers libcppnetlib-server-parsers.a)
find_library(cppnetlib-uri libcppnetlib-uri.a)
find_library(cppnetlib-client-connections libcppnetlib-client-connections.a)
# pthread
find_library(pthread libpthread.a)
# boost libraries
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
# set -static, when STATIC_LINKING is TRUE and set LINK_SEARCH_END_STATIC
# to remove the additional -bdynamic from the linker line.
IF(STATIC_LINKING)
SET(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++"
SET_TARGET_PROPERTIES(pvqa-server PROPERTIES LINK_SEARCH_END_STATIC 1)
ENDIF(STATIC_LINKING)
Related
I have application built in Ubuntu 14.04 / GCC 4.8 / CMake 2.8 with -static-libgcc and -static-libstdc++. ldd command shows:
linux-vdso.so.1 => (0x00007ffc16195000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcb18256000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcb17f50000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcb17b8a000)
/lib64/ld-linux-x86-64.so.2 (0x000055fc367e1000)
When I add -static to CMAKE_EXE_LINKER_FLAGS - I got different output from ldd:
linux-vdso.so.1 => (0x00007ffd48349000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe8d0335000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe8d0031000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe8cfd2a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe8cf965000)
/lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x000055d61ec7d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe8cf74f000)
I supposed -static flag will result in static linked application. It did not happen.
May anyone explain me why ?
I have makefile generated by Qt Creator - with -static switch build results in fully static application.
-static is different from -static-libgcc and -static-libstdc++ specifically in that -static does not assume static linking of standard libraries. Those two flags control this behavior.
To link statically with standard libraries, add those two flags to CMake CMAKE_EXE_LINKER_FLAGS variable.
It is from resulting CMakeLists.txt. I use -DSTATIC_LINKING=True when running CMake.
IF(STATIC_LINKING)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
ENDIF(STATIC_LINKING)
# libpcap library
find_library(pcap libpcap.a)
# opencore-amr libraries
find_library(opencore-amrnb libopencore-amrnb.a)
find_library(opencore-amrwb libopencore-amrwb.a)
# c++ netlib
find_library(cppnetlib-server-parsers libcppnetlib-server-parsers.a)
find_library(cppnetlib-uri libcppnetlib-uri.a)
find_library(cppnetlib-client-connections libcppnetlib-client-connections.a)
# pthread
find_library(pthread libpthread.a)
# boost libraries
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
# set -static, when STATIC_LINKING is TRUE and set LINK_SEARCH_END_STATIC
# to remove the additional -bdynamic from the linker line.
IF(STATIC_LINKING)
SET(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++"
SET_TARGET_PROPERTIES(pvqa-server PROPERTIES LINK_SEARCH_END_STATIC 1)
ENDIF(STATIC_LINKING)
I'm using CLion IDE, Cmake with GCC compiler and I'm trying to run binary with address sanitizer. I followed this: What's the proper way to enable AddressSanitizer in CMake that works in Xcode and added
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
to CMakeLists.txt file and then got an error when running the binary in CLion using the run button:
==22084==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.
When I run the binary via terminal, it works as it should be. It doesn't work in CLion's run window. https://imgur.com/a/MXOM7BJ
Edit: The solution was adding LD_PRELOAD=libasan.so.5 to environment variables of CLion.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(Playground)
set(CMAKE_CXX_STANDARD 14)
add_executable(Playground main.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -g")
What's happening?
Adding these lines from the stackoverflow thread to CMakeLists.txt file didnt work too.
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-fsanitize=address -fno-omit-frame-pointer")
and this line from this guide https://www.jetbrains.com/help/clion/google-sanitizers.html#LSanChapter also didnt work
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O1")
Then I tried adding LD_TRACE_LOADED_OBJECTS=1 to CLion environment variables and nothing changed.
My ldd ./file command output of the binary:
linux-vdso.so.1 (0x00007ffd82d96000)
libgtk3-nocsd.so.0 => /lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007f7d532e1000)
libasan.so.5 => /lib/x86_64-linux-gnu/libasan.so.5 (0x00007f7d528af000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7d526bd000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7d526b7000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7d52694000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7d52689000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7d52538000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7d5251d000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7d5350c000)
First remove all of the set commands you added, and then add these commands before the compile:
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
The problem is caused by the LD_PRELOAD=libgtk3-nocsd.so.0 setting somewhere in your environment. Overwrite it or unset the variable in your CLion run configuration.
I want to build a c++ program that dynamically links the QT-Core library.
For this I am using the WSL as my build environment and CLion as my IDE.
When I compile this programm in the WSL (ubuntu_18.04) the linker does not find the QtLibrary but when I compile it in a docker container (ubuntu_18.04) the linker finds the library.
I am quite confused by this since it seems to me I have set the library search path correctly.
Anyone got any idea what might be causing this ?
My project structure is the following:
apps
- CMakeLists.txt
- main.cpp
extern
- qt-linux
src
- CMakeLists.txt
- functions.cpp
- functions.hpp
toolschains
- linux-toolchain.cmake
CMakeLists.txt
build.sh
The CMakeLists.txt files look like this:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(cpp_hello_world)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/Install)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
# QT SETUP
if(UNIX)
set(Qt5Core_DIR "extern/qt-linux/lib/cmake/Qt5Core")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/extern/qt-linux/lib/ DESTINATION
lib)
endif()
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5Core)
add_subdirectory(src)
add_subdirectory(apps)
src/CMakeLists.txt:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_library(HelloLibrary SHARED functions.hpp functions.cpp)
target_link_libraries(HelloLibrary Qt5::Core)
target_include_directories(HelloLibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
install(TARGETS HelloLibrary DESTINATION lib)
install(FILES functions.hpp DESTINATION include)
apps/CMakeLists.txt:
add_executable(hello-world main.cpp)
target_link_libraries(hello-world HelloLibrary)
target_link_libraries(hello-world -static-libgcc -static-libstdc++)
install(TARGETS hello-world DESTINATION bin)
and I build using the following build.sh script:
#!/bin/bash
export SOURCE_DIR=$(pwd)
rm -R build
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=../toolchains/linux-
toolchain.cmake -G "CodeBlocks - Unix Makefiles" ${SOURCE_DIR}
make
make install
ldd output WSL:
linux-vdso.so.1 (0x00007ffff62d6000)
libHelloLibrary.so => /mnt/c/Users/ci/Documents/Development/cpp-cmake-prototype/Install/bin/../lib/libHelloLibrary.so (0x00007f7c96fb0000)
libQt5Core.so.5 => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7c96bb0000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7c97600000)
libQt5Core.so.5 => not found
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7c96820000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7c965f0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7c96250000)
ldd output Docker-Container:
linux-vdso.so.1 (0x00007ffc6932a000)
libHelloLibrary.so => /Install/bin/./../lib/libHelloLibrary.so (0x00007f36411db000)
libQt5Core.so.5 => /Install/bin/./../lib/libQt5Core.so.5 (0x00007f3640c33000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3640842000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3641715000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f36404b9000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f36402a1000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3640082000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f363fe65000)
libicui18n.so.60 => /usr/lib/x86_64-linux-gnu/libicui18n.so.60 (0x00007f363f9c4000)
libicuuc.so.60 => /usr/lib/x86_64-linux-gnu/libicuuc.so.60 (0x00007f363f60d000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f363f409000)
libdouble-conversion.so.1 => /usr/lib/x86_64-linux-gnu/libdouble-conversion.so.1 (0x00007f363f1f8000)
libglib-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f363eee2000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f363eb44000)
libicudata.so.60 => /usr/lib/x86_64-linux-gnu/libicudata.so.60 (0x00007f363cf9b000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f363cd29000)
I figured this one out now
The problem was the libQt5Core.so.5 was build with .note.ABI-tag set to a version incompatible with what the WSL identifies as(4.4) but the docker container was compatible(4.9).
Similar to what happens here:
https://github.com/Microsoft/WSL/issues/3023
I use CMAKE to link a program to SDL2, OpenGL and I compile tinyxml2 as a shared library. The resulting program binary is 1.4 mb but there's barely 1k lines of code in the program. I suspect some library is statically linked. I'd prefer they are linked as shared libraries. I tried with debug symbols on and off with:
cmake -DCMAKE_BUILD_TYPE=Debug .
and off:
cmake .
and resulting binaries are still 1.4 mb each, which is weird because debug should be bigger. Here is my CMakeLists.txt:
project(ProjectName)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake-find-scripts")
aux_source_directory(. SRC_ROOT)
aux_source_directory(./extlib SRC_EXTLIB)
aux_source_directory(./engine SRC_ENGINE)
aux_source_directory(./utils SRC_UTILS)
include_directories(./extlib)
include_directories(./engine)
include_directories(./utils)
include_directories(${SDL2_INCLUDE_DIR})
include_directories(${OPENGL_INCLUDE_DIRS})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
set(LONE_HEADERS ./engine/stc_config.h)
add_executable(${PROJECT_NAME} ${SRC_ROOT} ${SRC_ENGINE} ${SRC_UTILS} ${LONE_HEADERS})
find_package(SDL2 REQUIRED)
find_package(SDL2IMAGE REQUIRED)
find_package(OpenGL REQUIRED)
add_library(tinyxml2 SHARED ./extlib/tinyxml2.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} ${OPENGL_LIBRARIES} ${SDL2IMAGE_LIBRARIES} tinyxml2)
Edit: I've checked which library files the binary is linked with using ldd command and the output confirms they are all shared libraries:
linux-vdso.so.1 (0x00007fff9dbf0000)
libSDL2-2.0.so.0 => /usr/lib/libSDL2-2.0.so.0 (0x00007f3accff7000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f3accdda000)
libGLU.so.1 => /usr/lib/libGLU.so.1 (0x00007f3accb59000)
libGL.so.1 => /usr/lib/libGL.so.1 (0x00007f3acc7fb000)
libSDL2_image-2.0.so.0 => /usr/lib/libSDL2_image-2.0.so.0 (0x00007f3acc5d5000)
libtinyxml2.so => /home/hacow/OGLTEST/libtinyxml2.so (0x00007f3acc3bd000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f3acc0ae000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007f3acbda9000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f3acbb93000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f3acb7f0000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f3acb5ec000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007f3acb3e4000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3acd2fa000)
libnvidia-tls.so.349.16 => /usr/lib/libnvidia-tls.so.349.16 (0x00007f3acb1e1000)
libnvidia-glcore.so.349.16 => /usr/lib/libnvidia-glcore.so.349.16 (0x00007f3ac8472000)
libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f3ac8130000)
libXext.so.6 => /usr/lib/libXext.so.6 (0x00007f3ac7f1e000)
libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007f3ac7cfc000)
libXau.so.6 => /usr/lib/libXau.so.6 (0x00007f3ac7af8000)
libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007f3ac78f2000)
Building with cmake . uses the same CMAKE_BUILD_TYPE as the most recent build in that directory. Use cmake -DCMAKE_BUILD_TYPE=Release . to build in release mode. If you built debug first then release, using the procedure you described, it would just rebuild your debug binary.
My cmake file is
cmake_minimum_required(VERSION 2.8.4)
project(libtry CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(EXE_NAME libtry)
file(GLOB_RECURSE MAIN_SRC_FILES "src/*.cpp")
add_library (Try SHARED ${MAIN_SRC_FILES})
set(SOURCE_FILES main.cpp)
add_executable(${EXE_NAME} ${SOURCE_FILES})
target_link_libraries(${EXE_NAME} Try)
This file works and creates the two files: a .so file and an executable and it works perfectly. The problem is even after removing the .so file, the executable is working normally which means that the executable is statically linked.
Why this is happening and why cmake is not dynamically using the .so file?
Update
The dependencies by running ldd confirms this. Output of ldd is
linux-vdso.so.1 => (0x00007fffea5fe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f3585779000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f3585563000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f358519c000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f3584e96000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3585aa7000)