Cannot properly include header file in executable file via CMake - c

I'm trying to run a project with such structure:
I have this CMake code:
cmake_minimum_required(VERSION 3.19)
project(Random C)
set(CMAKE_C_STANDARD 99)
add_executable(Main usage.c include/random.h source/random.c)
include_directories(include)
And I have this error in random.c:
BUT I don't have it in usage.c:
What is going on here?

Related

Trying to use Paho C client library for MQTT with Cmake

I have a simple test project to try and include the mqtt library but cant seem to get it working.
Here's my directory structure:
test/
build/
paho.mqtt.c/
CMakeLists.txt
main.c
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name
project(mqtt)
add_executable(${PROJECT_NAME} main.c)
add_subdirectory(paho.mqtt.c)
target_include_directories(
${PROJECT_NAME}
PUBLIC paho.mqtt.c/src
)
target_link_directories(
${PROJECT_NAME}
PUBLIC paho.mqtt.c/src
)
target_link_libraries(${PROJECT_NAME}
paho.mqtt.c
)
When I try and include MQTTAsync.h into main.c the output from my terminal reads
/usr/bin/ld: cannot find -lpaho.mqtt.c
If anyone can help it would be greatly appreciated. I'm new with CMake and C programming so forgive me for being a noob.

CMAKE C Macro not propagating to include directory

I have a Open source C library I want to compile but to set the compiler to recognize OpenMPI is have to set the C macro PARALLEL to 1 so that in the header files the:
#ifdef PARALLEL
#include <mpi.h>
#endif
Will execute. Below is the CMAKE file I'm dealing with where it adds the src directory as a subdirectory and the header file as an include_directory.
Using this sets the C macro PARELLEL correctly for everything in the src directory. However, everything in the include directory is left with PARALLEL undefined.
cmake_minimum_required(VERSION 3.14)
project(SDFC VERSION 14.4.2 LANGUAGES C)
option(PARALLEL "Using MPI" ON)
set(TOPLEVEL "${CMAKE_CURRENT_SOURCE_DIR}")
#file(GLOB SOURCE_ALL include/*inc CMakeLists.txt Makefile*)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpic -g")
if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -gdwarf-2")
endif()
set(SDFC_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" CACHE PATH
"Path to include files for SDF C library")
set(SDFC_LIB_DIR "${CMAKE_CURRENT_BINARY_DIR}/src" CACHE PATH
"Path to build directory for SDF C library")
include_directories(include)
add_subdirectory(src)
if(PARALLEL)
find_package(MPI REQUIRED)
include_directories(${MPI_C_INCLUDE_PATH})
add_compile_definitions(-DPARALLEL)
#add_compile_definitions(-DPARALLEL)
include(CMake/CheckMPIVersion.cmake)
endif()
install(DIRECTORY include DESTINATION . COMPONENT develop PATTERN uthash.h EXCLUDE)
install(FILES src/uthash/include/uthash.h DESTINATION include COMPONENT develop)
I use:
cmake .
make
Any help is appreciated.
So it turns out that just because a static library is compiled and built using CMAKE which sets the preprocessor macros for header file (say library.h):
#ifdef PARALLEL
#include <mpi.h>
#endif
Doesn't mean you can use the static library and header file in your new code without defining the preprocessor macro.
So after making this static library with:
cmake .
make
I got a library.a file
In order to link it to my new code, say main.c that uses #include "library.h", I had to not only include the library and header file directory in the command line but also had to define the preprocessor macro PARALLEL as follows:
gcc -DPARALLEL=1 -L/Path_To_Library_Directory -I/Path_To_Include_Directory main.c -o main -llibrary

How to include header files in external library in CMake

I have a C application which is running on Raspberry Pi 3 and currently, I have to build it on PI with Cmake. I am trying to build it on Ubuntu machine. I have added a CMAKE_TOOLCHAIN_FILE as described here.
I could run cmake. -DCMAKE_TOOLCHAIN_FILE without any problem but the "make" command is not successful and it can not find a header file inside one of the external library: "mirsdrapi-rsp". The error message is:
fatal error: mirsdrapi-rsp.h: No such file or directory
#include "mirsdrapi-rsp.h"
^
compilation terminated.
I have created a folder named "lib" and have put the "libmirsdrapi-rsp.so" file inside it.
my CMakeLists.txt is as below:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wextra -v -g -D_XOPEN_SOURCE")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -v ")
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-v")
set (SDR_API_PATH ${CMAKE_SOURCE_DIR}/lib)
include_directories (include ${SDR_API_PATH})
include_directories("${CMAKE_SOURCE_DIR}/lib")
find_library(mirslocation NAMES mirsdrapi-rsp HINTS ${SDR_API_PATH} NO_CMAKE_FIND_ROOT_PATH)
message(STATUS ${mirslocation})
add_library(mirs STATIC IMPORTED)
set_target_properties(mirs PROPERTIES IMPORTED_LOCATION ${mirslocation})
target_link_libraries (raspberryPiDaemon mirs)
target_link_libraries(raspberryPiDaemon m)
Cmake is printing the right path of the library mirsdrapi-rsp while running "find_library" and as I mentioned I am getting the error message just while running "make" command and not "cmake" command.
My content of CMAKE_TOOLCHAIN_FILE is as below:
# Define our host system
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
# Define the cross compiler locations
SET(CMAKE_C_COMPILER ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
# Define the sysroot path for the RaspberryPi distribution in our tools folder
SET(CMAKE_FIND_ROOT_PATH ${CMAKE_SOURCE_DIR}/../tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/arm-linux-gnueabihf/sysroot/SET)
# Use our definitions for compiler tools
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries and headers in the target directories only
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
add_definitions(-Wall -std=c11)
Anybody knows how I can add the header file from mirsdrapi-rsp library to include path?
I guess that you're setting include_directories to the wrong path (it is set 2 times to ${CMAKE_SOURCE_DIR}/lib which must be the folder of libraries not the header files). Check again the correct location of the missing header file.
More precisely: you need to find the path of mirsdrapi-rsp.h and let CMake know it just like for find_library:
find_path(MIRSDRAPI_INCLUDE_DIRS NAMES mirsdrapi-rsp.h PATHS {proper-location})
if (MIRSDRAPI_INCLUDE_DIRS)
target_include_directories(raspberryPiDaemon PRIVATE ${MIRSDRAPI_INCLUDE_DIRS})
endif()
In addition, you can set the INTERFACE_INCLUDE_DIRECTORIES property to the library like this:
set_property(TARGET mirsdrapi-rsp APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${MIRSDRAPI_INCLUDE_DIRS})
This way, target_include_directories might be unnecessary and target_link_directories should be enough.

C - configure cmake to propely link ncursesw

I'm building a school project application which requires the use of ncurses library. While trying to use mvwaddwstr() function, I get the following error: undefined reference to `mvwaddwstr'.
My CMake configuration file is as follows:
cmake_minimum_required(VERSION 3.0)
set(CMAKE_C_STANDARD 11)
project(client)
# Libraries
find_package(Curses REQUIRED)
find_package(Threads REQUIRED)
include_directories(${CURSES_INCLUDE_DIRS})
# Sources
set(CLIENT_SOURCE_FILES
main.c)
# Links
add_library(client_library ${CLIENT_SOURCE_FILES})
add_executable(client ${CLIENT_SOURCE_FILES})
target_link_libraries(client ${CURSES_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(client INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")
Edit
As suggested I tried adding:
set(CURSES_NEED_NCURSES TRUE)
set(CURSES_NEED_WIDE TRUE)
include(FindCurses)
before the find_package line but it still ain't working.

C: How to add Ncurses and Math Flags into CMakeLists.txt

I'm importing a C project into the Clion IDE, that uses Cmake.
My project uses these external libraries: Math and Ncurses.
However i'm not able to get the script CMakeLists.txt working.
This is the script:
cmake_minimum_required(VERSION 2.8.4)
project(thegame)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lncurses -lm")
set(SOURCE_FILES thegame.c ./src/headers/set.h ./src/headers/functions.h ./src/headers/game.h ./src/headers/heap.h ./src/headers/lib.h
./src/headers/parser.h ./src/headers/pathfind.h ./src/headers/structures.h
./src/functions.c ./src/game.c ./src/heap.c ./src/lib.c ./src/parser.c ./src/pathfind.c ./src/set.c
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "~/Binaries")
add_executable(thegame ${SOURCE_FILES})
I'm getting a lot of messages like this:
thegame.c:(.text.startup+0x1c): undefined reference to "mousemask"
...
What I'm doing Wrong?

Resources