Cmake, How to add link option like '-rdynamic' in debug build - cmake

I added -rdynamic option in debug build in CMakeLists.txt like following.
Is there a better or simple way to this?
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_link_libraries(${PROJECT_NAME}
-rdynamic -lm -lpthread -lboost_system
)
else()
target_link_libraries(${PROJECT_NAME}
-lm -lpthread -lboost_system
)
endif()
There is an similar question at What is the modern method for setting general compile flags in CMake? And that answer is a general solution.
But I need a more specific answer for this question.

Related

What is a correct way to solve undefined reference to undefined reference to `LLVMInitializeAArch64TargetInfo'

In Clion when i build llvm project I got following message:
CMakeFiles/codegen_llvm.dir/main.cpp.o: In function `llvm::InitializeAllTargetInfos()':
/usr/lib/llvm-9/include/llvm/Config/Targets.def:26: undefined reference to `LLVMInitializeAArch64TargetInfo'
/usr/lib/llvm-9/include/llvm/Config/Targets.def:27: undefined reference to `LLVMInitializeAMDGPUTargetInfo'
/usr/lib/llvm-9/include/llvm/Config/Targets.def:28: undefined reference to `LLVMInitializeARMTargetInfo'
/usr/lib/llvm-9/include/llvm/Config/Targets.def:29: undefined reference to `LLVMInitializeBPFTargetInfo'
/usr/lib/llvm-9/include/llvm/Config/Targets.def:30: undefined reference to `LLVMInitializeHexagonTargetInfo'
/usr/lib/llvm-9/include/llvm/Config/Targets.def:31: undefined reference to `LLVMInitializeLanaiTargetInfo'
And My CmakeLists is here:
cmake_minimum_required(VERSION 3.15)
project(codegen_llvm)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
set(CMAKE_CXX_STANDARD 14)
add_executable(codegen_llvm main.cpp)
llvm_map_components_to_libnames(llvm_libs support core irreader executionEngine)
target_link_libraries(codegen_llvm ${llvm_libs})
...
What should I do?
cmake solution
cmake_minimum_required(VERSION 3.10)
project(project_name )
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
link_libraries()
execute_process(COMMAND llvm-config --libs OUTPUT_VARIABLE LIBS)
execute_process(COMMAND llvm-config --system-libs OUTPUT_VARIABLE SYS_LIBS)
execute_process(COMMAND llvm-config --ldflags OUTPUT_VARIABLE LDF)
#message(STATUS "Found LLVM" ${LIBS})
string(STRIP ${LIBS} LIBS)
string(STRIP ${SYS_LIBS} SYS_LIBS)
string(STRIP ${LDF} LDF)
link_libraries(${LIBS} ${SYS_LIBS} ${LDF})
execute_process(COMMAND llvm-config --cxxflags OUTPUT_VARIABLE CMAKE_CXX_FLAGS)
string(STRIP ${CMAKE_CXX_FLAGS} CMAKE_CXX_FLAGS)
add_executable(project_name toy.cpp)
one liner
clang++ -g toy.cpp $(llvm-config --cxxflags --ldflags --system-libs --libs) -O3 -o toy
If you still do not have a solution for this error, try to add ${LLVM_TARGETS_TO_BUILD} to your link libraries.
I am using CLion too, and I solved this by changing:
llvm_map_components_to_libnames(llvm_libs support core irreader codegen mc mcparser option)
to:
llvm_map_components_to_libnames(llvm_libs ${LLVM_TARGETS_TO_BUILD} support core irreader codegen mc mcparser option)
In your last but one line config is not an llvm component, so you'd want to remove it. To see the list of official components, use the llvm-config --components command.

C Cmake setup - undefined reference to pow() (despite -lm)

I'm trying to build a project (with CLion) with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(alfa_1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11 -Wall -Wextra -lm")
set(SOURCE_FILES
src/foo.h
src/foo.c
src/bar.h
src/bar.c
src/parser.h
src/parser.c)
add_executable(alfa_1 ${SOURCE_FILES})
In foo.c I use pow() function from math.h, which I include in foo.h. And obviously foo.h is included in foo.c. In bar.c I have main that is doing nothing. Now, standard command line compilation like this
gcc -o bar bar.c bar.h foo.h foo.c -lm
works fine but building the project yields undefined reference to pow. As one can see I included -lm flag in CmakeLists.txt file, so I don't get why this linking part is not working here
CMAKE_CXX_FLAGS are flags for the C++ compiler. -l is a linker flag. To link to a library, use this:
target_link_libraries(alfa_1 m)
You might also want to replace -std=c11 with use of CMAKE_C_STANDARD and related variables (CMAKE_C_STANDARD_REQUIRED, CMAKE_C_EXTENSIONS), and possibly replace use of CMAKE_CXX_FLAGS with a call to target_compile_options().
add the link of library in the cmakeList
add_library(math STATIC path/to/file.cpp)
add_executable(cmake_hello main.cpp)
target_link_libraries(cmake_hello math)
and in the class cpp
#include "path/to/file.hpp"
for mode detaills see this link
In Clion this configuration solved my problem:
cmake_minimum_required(VERSION 3.13)
project(K_Nearest C)
set(CMAKE_C_STANDARD 99)
add_executable(K_Nearest main.c point.h point.c group.c group.h)
target_link_libraries(K_Nearest m)
The important step was: target_link_libraries(K_Nearest m), it should come after add_executable(...) statement.

cmake link time optimisation with clang

I'm trying to use LTO in clang (with cmake): flag -emit-llvm set in compile time, output generated with -c flag, so LLVM bitcode is the result. The problem is, cmake give me result with ".o" suffix, but i need ".bc". I found other codes that used this: set(CMAKE_C_OUTPUT_EXTENSION .bc), but it didn't work.
cmake_minimum_required(VERSION 3.3)
project(cc C)
set(CMAKE_BUILD_TYPE Release)
enable_language(C)
file(GLOB_RECURSE SOURCES "src/*.c")
add_executable(cc ${SOURCES})
if (UNIX)
target_compile_options(cc PRIVATE "-Weverything")
target_compile_options(cc PRIVATE "-std=c11")
target_compile_options(cc PRIVATE "-emit-llvm")
set(CMAKE_C_FLAGS_RELEASE "-Ofast -DNDEBUG")
set(CMAKE_C_OUTPUT_EXTENSION ".bc")
endif()

CMake, static library and link time optimization

I'm trying to create static library with link time optimization using cmake and g++.
set(
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -pedantic -std=c++11"
)
if (CMAKE_COMPILER_IS_GNUCXX)
set(
CMAKE_STATIC_LINKER_FLAGS_RELEASE
"${CMAKE_STATIC_LINKER_FLAGS_RELEASE} -flto -fwhole-program"
)
endif()
add_library(
mylib STATIC
mylib.cpp
)
But when running typical
cmake -DCMAKE_BUILD_TYPE=Release ..
make
I'm getting following error:
/usr/bin/ar: two different operation options specified
link.txt file contains following commands:
/usr/bin/ar cq libmylib.a -flto -fwhole-program CMakeFiles/mylib.cpp.o
/usr/bin/ranlib libmylib.a
From what I understand from running ar --help the -flto -fwhole-program should be before libmylib.a in the first line. But I have no idea how to force CMake to put it there.
Is my assumption correct? And how can I resolve this?
EDIT: I'd like to add that I'm completely new to using LTO so if it doesn't make sense to use it for static libraries, please do tell me so.
-flto isn't a valid option for ar. You should instead use these flags for CMAKE_EXE_LINKER_FLAGS.

How to stop CMake from linking against libstdc++

I have a very simple CMakeLists.txt for a C++ project, which builds a shared library:
add_library(foo SHARED
${HDR_PUBLIC}
${SOURCES})
When linking the library, CMake automatically uses -lstdc++. How can I stop it from doing this?
You can add -stdlib=libc++ to compiler flags.
Simple example:
cmake_minimum_required(VERSION 2.8.4)
project(test)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")
add_executable(test main.cpp)
Give output:
"/usr/bin/ld" ... -o test ... -lc++ ...
By default:
cmake_minimum_required(VERSION 2.8.4)
project(test)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v")
add_executable(test main.cpp)
Link to stdc++:
"/usr/bin/ld" ... -o test ... -lstdc++ ...
[update]
If you don't need to link to c++ lib at all - use '-nodefaultlibs' as linker flag and '-nostdinc++' to compiler flag. You may need to link some default libraries, like '-lSystem'.
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
set_target_properties(yourtarget PROPERTIES LINKER_LANGUAGE C)
Source: http://cmake.3232098.n2.nabble.com/setting-LINKER-LANGUAGE-still-adds-lstdc-td7581940.html