How to Add "-L" (Ell) Compiler Flag in Cmake

How to add -l (ell) compiler flag in CMake

Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:

target_link_libraries(day_g pq)

Unable to include compile flag with CMAKE 3.15.0 [duplicate]

To add the -lws2_32 linker flag in modern CMake, the best approach is to use target_link_libraries. I've updated your example below:

cmake_minimum_required (VERSION 3.14.0)
project (BoostCMakeTutorial)
add_executable(BoostCMakeTutorial boostTcpClient.cpp)
target_link_libraries(BoostCMakeTutorial ws2_32)

How to set compiler specific flags in cmake

You can set the variables CMAKE_CXX_FLAGS_RELEASE (applies to release builds only), CMAKE_CXX_FLAGS_DEBUG (applies to debug builds only) and CMAKE_CXX_FLAGS (applies to both release and debug).
In case you use also other compilers, you should only set the options for msvc:

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:latest")
endif()

How do I add a linker or compile flag in a CMake file?

Note: Given CMake evolution since this was answer was written in 2012, most of the suggestions here are now outdated/deprecated and have better alternatives.


Suppose you want to add those flags (better to declare them in a constant):

SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
SET(GCC_COVERAGE_LINK_FLAGS "-lgcov")

There are several ways to add them:

  1. The easiest one (not clean, but easy and convenient, and works only for compile flags, C & C++ at once):

     add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
  2. Appending to corresponding CMake variables:

     SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
  3. Using target properties, cf. doc CMake compile flag target property and need to know the target name.

     get_target_property(TEMP ${THE_TARGET} COMPILE_FLAGS)
    if(TEMP STREQUAL "TEMP-NOTFOUND")
    SET(TEMP "") # Set to empty string
    else()
    SET(TEMP "${TEMP} ") # A space to cleanly separate from existing content
    endif()
    # Append our values
    SET(TEMP "${TEMP}${GCC_COVERAGE_COMPILE_FLAGS}" )
    set_target_properties(${THE_TARGET} PROPERTIES COMPILE_FLAGS ${TEMP} )

Right now I use method 2.

How to use FFTW library in cmake?

We delegate this to pkg-config:

find_package(PkgConfig REQUIRED)
pkg_search_module(FFTW REQUIRED fftw3 IMPORTED_TARGET)
include_directories(PkgConfig::FFTW)
link_libraries (PkgConfig::FFTW)

This works with cmake 3.11 (at least, it may work with earlier versions too).


NOTE: This doesn't work with fftw3_thread component because they don't have a separate .pc file. (see https://github.com/FFTW/fftw3/issues/180).

This may work to add the component (not tested, doesn't work in Macs --see comments--):

link_libraries     (PkgConfig::FFTW -lfftw3_thread)

NOTE 2: I am pasting here @OlafWilkocx solution to get the thread component as well

    cmake_minimum_required(VERSION 3.20)
...
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-math-errno -ffinite-math-only") # clang
find_package(OpenMP REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(FFTW IMPORTED_TARGET REQUIRED fftw3)

if( NOT FFTW_ROOT AND DEFINED ENV{FFTWDIR} )
set( FFTW_ROOT $ENV{FFTWDIR} )
endif()

find_library(
FFTW_DOUBLE_THREADS_LIB
NAMES "fftw3_threads"
PATHS ${PKG_FFTW_LIBRARY_DIRS} ${LIB_INSTALL_DIR}
)

if (FFTW_DOUBLE_THREADS_LIB)
set(FFTW_DOUBLE_THREADS_LIB_FOUND TRUE)
set(FFTW_LIBRARIES ${FFTW_LIBRARIES} ${FFTW_DOUBLE_THREADS_LIB})
add_library(FFTW::DoubleThreads INTERFACE IMPORTED)
set_target_properties(FFTW::DoubleThreads
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFTW_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${FFTW_DOUBLE_THREADS_LIB}"
)
else()
set(FFTW_DOUBLE_THREADS_LIB_FOUND FALSE)
endif()

include_directories(PkgConfig::FFTW)

add_executable(solver_step src/solver_step.cc)
target_link_libraries(solver_step PRIVATE OpenMP::OpenMP_CXX ${VTK_LIBRARIES} PkgConfig::FFTW ${FFTW_DOUBLE_THREADS_LIB})

NOTE 3

I am told that the line include_directories(PkgConfig::FFTW) is always incorrect and suggested to either only use link_libraries(PkgConfig::FFTW) or target_link_libraries(target_name PRIVATE PkgConfig::FFTW).

see here: Avoid bad include paths in CMake's pkg-config fallback

How to set library flags after source file in cmake?

If you need to link against hwloc library you might use target_link_libraries command:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11") # <== remove *-lhwloc*

set(SOURCE_FILES source.cpp)
add_executable(source ${SOURCE_FILES})

target_link_libraries(source hwloc) # <== add this line

How do you set GDB debug flag with cmake?

If you want to build for debug (including source information, i.e. -g) when compiling, use

cmake -DCMAKE_BUILD_TYPE=Debug <path>

If you want to build a release build, you can use

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo <path>

How to add linker directory in cmake? [duplicate]

As discussed in comments:

In your target CMakeLists.txt, the one with your

add_executable (${PROJECT_NAME} ...)

add a

target_link_libraries(${PROJECT_NAME} PUBLIC ssl crypto ...)

call to attach those library dependencies to your target



Related Topics



Leave a reply



Submit