How to debug in Clion using Cmake if project needs libraries - debugging

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.

Related

How to create a makefile for Keystone library using cmake? [duplicate]

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

Using CMakeLists causes error while compiling fine in commandline

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).

How to force cmake not to find mpicxx but only mpicc

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.

Linking a .s file with CMake

I have a c function that I'd like to use but thats compiled with the Intel compiler instead of the gnu C compiler. I'm using cmake to build the program.
(I'm actually using ROS and hence rosmake but the base is cmake so I think its more of a cmake issue than a ROS issue).
Suppose the file built with icc is x.c and produces an x.s file. I want to use the function a() from x.c in my file y.cpp.
In y.cpp I have:
#include "x.h"
.....
call a()
which works if CMakeLists.txt has
rosbuild_add_executable(y y.cpp x.c)
rosbuild_add_executable is analogous to add_executable(...)
but if I build x.c with icc and try to include the x.s file instead:
rosbuild_add_executable(y y.cpp x.s)
It doesnt work. Is there some change I should make to the way I call a() in y.cpp? or is there some other way to link it.
When using gcc, you can compile .S files with your C compiler (no explicit invocation of asm needed). CMake can be told to do so using
set_property(SOURCE <myfile>.S PROPERTY LANGUAGE C)
for each of your .S files.
To work with .s files you'll have to enable assembly language support in CMake with enable_language.
You can find more information here: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/languages/Assembler

Enable compiler warnings in Kdevelop

I'm using Kdevelop for a simple C++ project. I know that Kdevelop uses CMake to build the project, but the only thing that I known about CMake is that if I add a new .cpp source file in my project, I have to add it also in CMakeLists.txt.
Now I'm trying to enable tha gcc compiler warnings (i.e. compiling with g++ -Wall ...).
Does Kdevelop have a compiler settings section, or I have to edit directly the Makefile or another CMake settings file?
You can add compiler flags in CMake by adding the following command to your CMakeLists.txt:
list( APPEND CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

Resources