How can I get CMake to link pthread statically on Windows? I use MSYS2 MinGW 32 bit and cmake v3.7.
What I would like to achieve is a compiler invocation like
g++ -static-libgcc -static-libstdc++ -std=c++11 -o test test.cpp -Wl,-Bstatic -lpthread
Setting
target_link_libraries(test PUBLIC "-Wl,-Bstatic -lpthread")
results in -Wl,-Bdynamic -Wl,-Bstatic -lpthread being called. If I change CMAKE_EXE_LINKER_FLAGS, pthreads is included before my object files and thus symbols are not resolved.
find the Threads module:
find_package(Threads REQUIRED)
add_executable(myApp main.cpp)
target_link_libraries(myApp Threads::Threads)
Note from the documentation:
For systems with multiple thread libraries, caller can set CMAKE_THREAD_PREFER_PTHREAD
As the FindThreads.cmake mention in its source code:
# For systems with multiple thread libraries, caller can set
#
# ::
#
# CMAKE_THREAD_PREFER_PTHREAD
#
# If the use of the -pthread compiler and linker flag is preferred then the
# caller can set
#
# ::
#
# THREADS_PREFER_PTHREAD_FLAG
#
# Please note that the compiler flag can only be used with the imported
# target. Use of both the imported target as well as this switch is highly
# recommended for new code.
So in addition to what already said, you might need to set the additional flag THREADS_PREFER_PTHREAD_FLAG. In some systems (OSX, etc.) this flag is needed at compilation time because it defines some macros that would be missing if you would only link -lpthread.
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
add_library(test test.cpp)
set_property(TARGET test PROPERTY CXX_STANDARD 11)
set_target_properties(test PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(test PROPERTIES LINK_SEARCH_END_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
find_package(Threads REQUIRED)
target_link_libraries(test Threads::Threads)
Does it help?
Related
This question already has answers here:
Libraries in /usr/local/lib not found
(1 answer)
Why the installed program has error when loading shared library in linux (cmake)
(3 answers)
Closed 2 years ago.
I'm building a kmeans executable from this cmake file:
# check the minimum version
cmake_minimum_required( VERSION 3.16 )
# the project name
project( kmeans )
#########################################################################
#### Define all the global variables to compile the application
#########################################################################
set( APPLICATION_NAME "kmeans")
#########################################################################
#### Adjust compiling option for this application
#########################################################################
# force the Release build if not already set
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif(NOT CMAKE_BUILD_TYPE)
# setting common c++ flags
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp -pthread -std=c++17 -static-libgcc -DKMEANS_DATASET_PATH=\"\\\"${CMAKE_CURRENT_SOURCE_DIR}/dataset/\\\"\"")
# setting debug flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g3 -O0")
# setting release with debug info flags
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -march=native -mtune=native -g3 -O2")
# setting release flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native -mtune=native -O3")
#########################################################################
#### Find external libaries
#########################################################################
# Boost program options
find_package(Boost REQUIRED program_options)
# include the directories
include_directories(${Boost_INCLUDES} )
################################
#### Sources
################################
set( KMEANS_SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src)
set( KMEANS_HDR_PATH ${CMAKE_CURRENT_SOURCE_DIR}/include)
set( KMEANS_HDR_FILES
${KMEANS_HDR_PATH}/kmeans.h
)
set( KMEANS_SRC_FILES
${KMEANS_SRC_PATH}/main.cc
${KMEANS_SRC_PATH}/cluster.c
${KMEANS_SRC_PATH}/kmeans_clustering.c
)
include_directories( ${KMEANS_HDR_PATH} )
################################
#### Compilation
################################
#----- Set binary name for the mini-app
add_executable(${APPLICATION_NAME} ${KMEANS_SRC_FILES} ${KMEANS_HDR_FILES})
target_link_libraries(${APPLICATION_NAME} Boost::program_options)
################################
#### Install
################################
install( TARGETS ${APPLICATION_NAME} DESTINATION ${PROJECT_SOURCE_DIR}/bin )
After building the kmeans executable gets generated correctly inside the build dir (build/kmeans) and works perfectly. After a make install tho the generated binary (bin/kmeans) gives the following error ./kmeans: error while loading shared libraries: libboost_program_options.so.1.75.0: cannot open shared object file: No such file or directory. I tried to take a look at the install command doc but couldn't find any option taking care of the case. I suppose I'm missing something inside the cmake file but cannot spot it.
I am facing below issue while running my build:
C:/Test.cpp: In member function '........':
C:/Test.cpp:291:50: error: 'round_one' may be used uninitialized in this function [-Werror=maybe-uninitialized]
I tried to grep for string maybe-uninitialized in my whole source code but I could not find one. I was expecting some declaration like below:
set_source_files_properties(ROOT_DIR/Test.cpp PROPERTIES COMPILE_FLAGS "-Wno-maybe-uninitialized -Wno-misleading-indentation" )
or
SET(GCC_COVERAGE_COMPILE_FLAGS "-Wno-maybe-uninitialized")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
But I could not find any - please let me know how Compiler flags are set in CMAKE utility?
The warning -Wmaybe-uninitialized is one of those that are enabled
by -Wall.
-Wall is always specified by proficient programmers. Warnings will be converted
to errors by -Werror, so the flags -Wall -Werror will produce -Werror=maybe-uninitialized,
as per your diagnostic, if a potentially uninitialized variable is detected.
You will very likely find -Wall ... -Werror in the specified compiler flags in the relevant CMakeLists.txt
One way is setting the add compiler flag for the project:
cmake_minimum_required(VERSION 2.8)
# Project
project(008-compile-flags-01)
# Add compile flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHELLO_WORLD" CACHE STRING "Hello World Define." FORCE)
# Executable source files
set(executable_SOURCES src/main.cpp)
# Executable
add_executable(executable ${executable_SOURCES})
Other way is setting compiler flag for the target:
cmake_minimum_required(VERSION 3.2)
# Project
project(008-compile-flags-03)
# Executable source files
set(executable_SOURCES src/main.cpp)
# Executable
add_executable(executable ${executable_SOURCES})
# Add compile flag
target_compile_options(executable PRIVATE -DHELLO_WORLD)
Other way is using target_compile_features. I haven't used this before. Please see:
https://cmake.org/cmake/help/latest/command/target_compile_features.html
https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html
This scientific application I am using uses cmake to build its program. Attached is the CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(PROJECT_NAME myproject)
project(${PROJECT_NAME})
# Set verbose output while testing CMake
#set(CMAKE_VERBOSE_MAKEFILE 1)
# Set CMake behavior
cmake_policy(SET CMP0004 OLD)
# Get DOLFIN configuration data (DOLFINConfig.cmake must be in
# DOLFIN_CMAKE_CONFIG_PATH)
find_package(DOLFIN)
# Need to get VTK config because VTK uses advanced VTK features which
# mean it's not enough to just link to the DOLFIN target. See
# http://www.vtk.org/pipermail/vtk-developers/2013-October/014402.html
find_package(VTK HINTS ${VTK_DIR} $ENV{VTK_DIR} NO_MODULE QUIET)
# Default build type (can be overridden by user)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
"Choose the type of build, options are: Debug MinSizeRel Release RelWithDebInfo." FORCE)
endif()
# Compiler definitions
add_definitions(${DOLFIN_CXX_DEFINITIONS})
# Compiler flags
set(CMAKE_CXX_FLAGS "${DOLFIN_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
# Include directories
include_directories(${DOLFIN_INCLUDE_DIRS})
include_directories(SYSTEM ${DOLFIN_3RD_PARTY_INCLUDE_DIRS})
# Executable
add_executable(${PROJECT_NAME} main.cpp)
# Target libraries
target_link_libraries(${PROJECT_NAME} ${DOLFIN_LIBRARIES})
However, I need to include some additional compiler flags and libraries not included within DOLFIN. I need to incorporate this package called PAPI. On my system, the paths/environments are:
PAPI_LIBDIR=/project/cacds/apps/papi/5.4.0/lib
PAPI_INCLUDE=/project/cacds/apps/papi/5.4.0/include
PAPI_FLAG=-lpapi
Normally I would do something like this:
gcc -O3 -o -I myproject $(PAPI_LIBDIR) main.c -L $(PAPI_INCLUDE) $(PAPI_FLAG)
But again I need the additional DOLFIN libraries and third party packages. Pardon me if this is a silly or simple question, but how would I modify the above CMakeLists.txt? I am kind of new to cmake, so any help appreciated, thanks
The cleanest solution would be to create a FindPAPI.cmake. So, add to your project this file inside of a cmake/folder:
FindPAPI.cmake
# Try to find PAPI headers and libraries.
#
# Usage of this module as follows:
#
# find_package(PAPI)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
# PAPI_PREFIX Set this variable to the root installation of
# libpapi if the module has problems finding the
# proper installation path.
#
# Variables defined by this module:
#
# PAPI_FOUND System has PAPI libraries and headers
# PAPI_LIBRARIES The PAPI library
# PAPI_INCLUDE_DIRS The location of PAPI headers
find_path(PAPI_PREFIX
NAMES include/papi.h
)
find_library(PAPI_LIBRARIES
# Pick the static library first for easier run-time linking.
NAMES libpapi.a papi
HINTS ${PAPI_PREFIX}/lib ${HILTIDEPS}/lib
)
find_path(PAPI_INCLUDE_DIRS
NAMES papi.h
HINTS ${PAPI_PREFIX}/include ${HILTIDEPS}/include
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PAPI DEFAULT_MSG
PAPI_LIBRARIES
PAPI_INCLUDE_DIRS
)
mark_as_advanced(
PAPI_PREFIX_DIRS
PAPI_LIBRARIES
PAPI_INCLUDE_DIRS
)
Then, modify your CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(PROJECT_NAME myproject)
project(${PROJECT_NAME})
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(PAPI REQUIRED)
...
# Include directories
include_directories(${DOLFIN_INCLUDE_DIRS})
include_directories(SYSTEM ${DOLFIN_3RD_PARTY_INCLUDE_DIRS})
include_directories(${PAPI_INCLUDE_DIRS})
# Executable
add_executable(${PROJECT_NAME} main.cpp)
# Target libraries
target_link_libraries(${PROJECT_NAME} ${PAPI_LIBRARIES} ${DOLFIN_LIBRARIES})
I hope it resolves it! ;)
I want to not add boost.cxx if cmake find_package found no boost installed. Does find_package return something that I can wrap in condition to compile boost.cxx or not. Here is my current cmake file:
add_executable (complex complex.cxx lexer.cxx boost.cxx ../../src/lili.cxx ../../src/lilu.cxx)
# Make sure the compiler can find all include files
include_directories (../../src)
include_directories (.)
# Make sure the linker can find all needed libraries
# rt: clock_gettime()
target_link_libraries(complex rt)
# Install example application
install (TARGETS complex
RUNTIME DESTINATION bin)
IF(UNIX)
find_package(Boost COMPONENTS system filesystem REQUIRED)
## Compiler flags
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-O2")
set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")
endif()
target_link_libraries(complex
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
#${PROTOBUF_LIBRARY}
)
ENDIF(UNIX)
The FindXXX scripts are supposed to set a variable <Packagename>_FOUND to TRUEif the package was found. So in your case, it will set Boost_FOUND if boost was found.
When compiling your Boost.cxx, I assume that you will need Boost headers as well, so you should adjust your include directories as well.*
look for Boost before creating your executable. Furhtermore, you need to set your include directories before adding the executable.
IF(UNIX)
find_package(Boost COMPONENTS system filesystem REQUIRED)
# IF( Boost_FOUND ) # checking this variable isnt even necessary, since you added
# REQUIRED to your call to FIND_PACKAGE
SET( BOOST_SRC_FILES boost.cxx )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} ) # you could move this down as well
# as ${Boost_INCLUDE_DIRS} will be
# empty if Boost was not found
# ENDIF()
ENDIF()
add_executable (complex complex.cxx lexer.cxx ${BOOST_SRC_FILES} ../../src/lili.cxx ../../src/lilu.cxx)
# Make sure the compiler can find all include files
include_directories (../../src)
include_directories (.)
# INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} ) # alternative location to
# add include dirs, see above
# Make sure the linker can find all needed libraries
# rt: clock_gettime()
target_link_libraries(complex rt)
# Install example application
install (TARGETS complex
RUNTIME DESTINATION bin)
IF(UNIX)
## Compiler flags
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-O2")
set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")
endif()
target_link_libraries(complex
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
#${PROTOBUF_LIBRARY}
)
ENDIF(UNIX)
Afternote: Since you use the REQUIRED flag when looking for Boost (since you only need it on Unix platform) it is even sufficient to use the optional-source-files-in-a-variable trick.
(*) Thanks to your question, I just found out that it doesn't matter whether include_directories(...) is called before or after creating the target with ADD_EXECUTABLE or ADD_LIBRARY since the directories are added to all targets in the same project.
Yes, if the find_package(Boost COMPONENTS system filesystem REQUIRED) succeeds, Boost_FOUND will be true.
Also, there will be component-specific versions, so Boost_date_time_FOUND, Boost_filesystem_FOUND, etc.
For further info, run
cmake --help-module FindBoost
Yes, it sets variable Boost_FOUND. Example from FindBoost.cmake:
== Using actual libraries from within Boost: ==
#
# set(Boost_USE_STATIC_LIBS ON)
# set(Boost_USE_MULTITHREADED ON)
# set(Boost_USE_STATIC_RUNTIME OFF)
# find_package( Boost 1.36.0 COMPONENTS date_time filesystem system ... )
#
# if(Boost_FOUND)
# include_directories(${Boost_INCLUDE_DIRS})
# add_executable(foo foo.cc)
# target_link_libraries(foo ${Boost_LIBRARIES})
# endif()
cmake version 2.8.5
I am trying to compile my project using cmake. However, when i compile I don't think I am including the debug cflags i.e. -ggdb -D_DEBUG. As when I try and debug there is no debub info.
Is there any problem with the CMakeLists.txt files. I have 3 of them
# Mimimum version of cmake required
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# Name of project
PROJECT(sdp_creator C)
# Check for correct compiler
# Using C compiler GNUCXX for c++ compiler
IF(CMAKE_COMPILER_IS_GNUCC)
MESSAGE(STATUS "=== GCC C COMPILER DETECTED")
SET(CMAKE_C_FLAGS "-m32 -ggdb -D_DEBUG -Wextra -Wall -Wunreachable-code -O0 -D_LARGEFILE64_SOURCE")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
# Using windows compiler i.e. msvc++
IF(WIN32)
MESSAGE(STATUS "=== MSVC COMPILER DETECTED")
ENDIF(WIN32)
# Location of directory where include files are kept
INCLUDE_DIRECTORIES($ENV{HOME}/projects/sdp_creator/src/sdp)
INCLUDE_DIRECTORIES($ENV{HOME}/projects/sdp_creator/src/apr/inc)
# Location of directory where libraries are kept
LINK_DIRECTORIES($ENV{HOME}/projects/sdp_creator/src/apr/lib)
# Add subdirectories
ADD_SUBDIRECTORY(driver)
ADD_SUBDIRECTORY(sdp)
building shared library:
# Create a shared library called libsdp from sdp.c
# NOTE: static is the default
# NOTE: the lib prefix is automatically added
ADD_LIBRARY(sdp SHARED sdp.c)
Creating executable:
# Add executable called sdp_creator from source file
ADD_EXECUTABLE(sdp_creator main.c)
# Link the sdp library and other libraries with the excutable
#if using windows compiler add additional windows libraries
IF(WIN32)
TARGET_LINK_LIBRARIES(sdp_creator libsdp ws2_32)
MESSAGE(STATUS "=== Linking executable with windows libraries")
ENDIF(WIN32)
# if using gcc compiler
# NOTE: no need to add the -l prefix i.e. -lsdp, no automatically
IF(CMAKE_COMPILER_IS_GNUCC)
TARGET_LINK_LIBRARIES(sdp_creator sdp apr-1)
MESSAGE(STATUS "=== Linking executable with posix libraries")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
Many thanks for any advice,
If you're using the "Unix Makefiles" (or any Makefile-based) generator, set the variable CMAKE_BUILD_TYPE to "Debug"
cmake -DCMAKE_BUILD_TYPE=Debug ../src
That will automatically add the right definitions and flags for your compiler. You should not have to add any flags yourself.
With multi-configuration generators, (like Visual Studio and Xcode), CMAKE_BUILD_TYPE is ignored, because the choice of whether to build a Debug or Release configuration is left up to the developer at build-time, and is not known at CMake configure time.
You can check the exact steps used in make by setting VERBOSE=1. That will tell you if the flags were included or not.
cmake project_dir
make VERBOSE=1
You can also check the CMakeCache.txt to see what value is assigned to CMAKE_C_FLAGS variable.
you can use the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS flags with -g0/1/2 (debug information flag for the compiler. -g2 is the highest information):
cmake ... -DCMAKE_C_FLAGS="-g2" -DCMAKE_CXX_FLAGS="-g2" ...
Another option, if unix Makefiles are used to build the project, is to set CMAKE_BUILD_TYPE in CMakeLists.txt file directly:
set(CMAKE_BUILD_TYPE Debug)
You can find more in
$ man cmakevars
Look for CMAKE_BUILD_TYPE