This question already has answers here:
How to link to the C math library with CMake?
(3 answers)
Closed 6 years ago.
How do I add flags like -lm(for math.h) in Clion to build and run a c file?
I basically want to use pow() function from math.h in my code and run and debug the same in Clion.
I'm new to CMake.
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(Assign2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.c)
add_executable(Assign2 ${SOURCE_FILES})
You need to add target_link_libraries(YOUR_TARGET_NAME_HERE m) to your CMakeLists.txt file.
(If you've tried that, or don't know what's the name of your target, please add the contents of your CMakeLists.txt file to your question)
Edit: your target name is Assign2, so at the bottom of your CMakeLists.txt, you need to add:
target_link_libraries(Assign2 m)
Related
I'm new in C. My code uses termcap library. And I'm trying to debug my code using Clion through Cmake Application. But it can't be compiled because functions that i use from a library are undefined. What should i add to CMakeLists.txt to debug my project?
My CMakeLists.txt now:
cmake_minimum_required(VERSION 3.21)
project(minishell C)
set(CMAKE_C_STANDARD 99)
add_executable(minishell main.c)
When i compile using clang i just do this:
clang main.c -ltermcap
and it works.
Can't understand what to do. Please help.
Work on Ubuntu 16
I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
Also I run only cmake file and get CMAKE_CXX_FLAGS with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
How properly add compiler flags to cmake file?
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)
-lq is not a compiler flag (CFLAGS) but a linker flag.
To pass a library in a CMake project you should use:
target_link_libraries(target_name libraries...)
Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.
... in your CMakeLists.txt the correct line to add is:
target_link_libraries(day_g pq)
Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:
make VERBOSE=1
I am compiling my code, in which I use posix threads in C.
I am using CLion and its CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(Test)
set(CMAKE_C_STANDARD 99)
add_definitions(-lpthread)
set(SOURCE_FILES main.c)
add_executable(Test ${SOURCE_FILES})
I am getting errors (eg: undefined reference tosem_init'`).
The proposed suggestion for solution is adding -lpthread compiler flag, but I already added it.
I compiled the same code from commandline:
gcc main.c -lpthread
It compiles without any problem.
What can be a possible problem/solution for this?
Remove add_definitions(-lpthread) entirely since pthread is not a definition, but a library dependency.
Add after add_executable():
target_link_libraries(Test pthread)
Also, if you want to see what commands CMake is using without having to examine its files, you can use it on the command line with cmake -DCMAKE_VERBOSE_MAKEFILE=ON ....
Btw, always prefer all the target_* commands, like target_compile_definitions() instead of older style add_definitions(). This keeps your project properties and dependencies clean and minimizes interference between different targets.
If after the above changes your code still does not compile, then it is highly likely the code itself is wrong (nothing to do with CMake).
I have a very simple CMake script. Unfortunately, the project uses a *.pde file which is plain C++ or C code.
CMake is working with any file ending, but I get a compiler error, because GCC does not know how to handle it. How can I add a global file extension to GCC, so that the *.pde file is compiled as a usual *.cpp file?
The -x c++ foo.pde command is nice if I want to use the console, but for CMake it is (I think) not applicable.
cmake_minimum_required(VERSION 2.8)
project(RPiCopter)
SET( RPiCopter
absdevice
containers
device
exceptions
navigation
frame
vehicle
receiver
scheduler
tinycopter.pde
)
message( STATUS "Include ArduPilot library directories" )
foreach( DIR ${AP_List} ${AP_List_Linux} ${AP_Headers} )
include_directories( "../libraries/${DIR}" )
endforeach()
include_directories( zserge-jsmn )
# ***************************************
# Build the firmware
# ***************************************
add_subdirectory ( zserge-jsmn )
#set(CMAKE_CXX_FLAGS "-x c++ *.pde")
ADD_EXECUTABLE ( RPiCopter ${RPiCopter} )
target_link_libraries ( RPiCopter -Wl,--start-group ${AP_List} ${AP_List_Linux} jsmn -Wl,--end-group )
You should be able to use set_source_files_properties along with the LANGUAGE property to mark the file(s) as C++ sources:
set_source_files_properties(${TheFiles} PROPERTIES LANGUAGE CXX)
As #steveire pointed out in his own answer, this bug will require something like the following workaround:
set_source_files_properties(${TheFiles} PROPERTIES LANGUAGE CXX)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_definitions("-x c++")
endif()
Normally you should be able to just extend CMAKE_CXX_SOURCE_FILE_EXTENSIONS. This would help, if you have a lot of files with unknown file extensions.
But this variable is not cached - as e.g. CMAKE_CXX_FLAGS is - so the following code in CMakeCXXCompiler.cmake.in will always overwrite/hide whatever you will set:
set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP)
I consider this non-caching being a bug in CMake, but until this is going to be changed I searched for a workaround considering the following:
You normally don't want to change files in your CMake's installation
It won't have any effect if you change CMAKE_CXX_SOURCE_FILE_EXTENSIONS after project()/enable_language() (as discussed here).
I have successfully tested the following using one of the "hooks"/configuration variables inside CMakeCXXCompiler.cmake.in:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_SYSROOT_FLAG_CODE "list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS pde)")
project(RPiCopter CXX)
message("CMAKE_CXX_SOURCE_FILE_EXTENSIONS ${CMAKE_CXX_SOURCE_FILE_EXTENSIONS}")
add_executable(RPiCopter tinycopter.pde)
I decided to use this approach. I just remove the file ending by cmake in the temporary build directory.
So GCC is not confused anymore because of the strange Arduino *.pde file extension.
# Exchange the file ending of the Arduino project file
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tinycopter.pde ${CMAKE_CURRENT_BINARY_DIR}/tinycopter.cpp)
CMake doesn't do this for you:
http://public.kitware.com/Bug/view.php?id=14516
I am trying to use cmake to generate Makefile for a MPI program. The issue I have is that I only have mpicc installed (working correctly) the mpicxx is in the PATH but is from an other installation (there are multiple MPIs installed on my system). I don't want to use mpicxx but only mpicc (and the include files and libraries of mpicc).
Bellow is a somewhat hardcoded effort and it works
cmake_minimum_required(VERSION 2.8)
PROJECT(mympihello)
ADD_EXECUTABLE(hellompi hellompi.c)
SET(CMAKE_C_COMPILER mpicc)
target_link_libraries(hellompi /export/home2/SEECS/bibrak/work/programs/installs/mpich3/include)
Following is the generic way (as far as I know) and I want to use this approach but forcing not to use mpicxx (not finding it, discarding mpicxx)
cmake_minimum_required(VERSION 2.8)
PROJECT(mympihello)
ADD_EXECUTABLE(hellompi hellompi.c)
# Require MPI for this project:
find_package(MPI REQUIRED)
set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})
include_directories(MPI_INCLUDE_PATH)
target_link_libraries(hellompi ${MPI_LIBRARIES})
Furthermore, it will be helpful to point me towards how to generate .so (shared library) for such programs that use MPI.
Thanks
Some how I managed to do it. Here is my solution to generating .so (shared library) using cmake but only using mpicc not mpicxx
cmake_minimum_required(VERSION 2.8)
project(projectname C)
SET( SOURCE_FILES
sourcefile1.c
sourcefile2.c
sourcefile3.c
)
ADD_LIBRARY(projectname SHARED ${SOURCE_FILES})
find_package(MPI)
if(MPI_FOUND)
set(CMAKE_C_COMPILE_FLAGS ${CMAKE_C_COMPILE_FLAGS} ${MPI_C_COMPILE_FLAGS})
set(CMAKE_C_LINK_FLAGS ${CMAKE_C_LINK_FLAGS} ${MPI_C_LINK_FLAGS})
include_directories(${MPI_C_INCLUDE_PATH})
endif(MPI_FOUND)
target_link_libraries(projectname ${MPI_C_LIBRARIES})
This generates a Makefile that creates projectname.so shared library.