How do I detect a debug build in my c source files with cmake? - c

I have this set in my CMakeLists.txt file.
SET (CMAKE_BUILD_TYPE "Debug")
However, this is not working in my C source files
#if defined(DEBUG)
// not getting here
#else
// getting here instead
#endif
What symbol if any are defined by setting CMAKE_BUILD_TYPE to Debug ?

Found the answer. It's definately a trap for those new to using cmake.
It seems that changing the build type in CMakeLists.txt will not change the build type if you've built it previously with another build type. The reason is because of it's cache.
Seeing as I am building out of source. i.e. I have a separate Build directory inside my source tree. I normally would run just "cmake .."
So clearing the Build tree and running "cmake .." again fixed it.
Now DEBUG is being defined for my source files and I can verify it with make VERBOSE=1
This link provides further detail and other options:
Understanding why CMAKE_BUILD_TYPE cannot be set

Related

How to inject variables into u-boot build at build time

I am a u-boot newbie.
I want to be able to make u-boot with a different build option at build time. E.g. I might have a build.sh like this:
source /path/to/target-toolchain
make distclean
make MyHardware_defconfig
make V=1
or even better:
make distclean
make MyHardware_defconfig
make CONFIG_ENABLE_CONSOLE=1 V=1
In MyHardware.c:
#if !defined CONFIG_ENABLE_CONSOLE
#error CONFIG_ENABLE_CONSOLE not defined!!!!!
#endif
I tried adding CONFIG_ENABLE_CONSOLE=1 to MyHardware_defconfig but when build get error line
I also tried make CONFIG_ENABLE_CONSOLE=1 V=1 but also same as above error.
How can I setup my project so I can build for both console enabled and disabled? Without having to hard code in the u-boot source code.
make MyHardware_defconfig
creates file .config. The usual way to edit it is calling
make menuconfig
If you want to modify .config in a script, you could use sed.
The configuration options are defined in files called Kconfig. https://www.kernel.org/doc/html/v5.18/kbuild/kconfig-language.html describes the syntax of Kconfig files.

CMake, how to set different value in a configure_file in Build vs Install

I have a simple CMake project with CTest and CPack. It uses the Lua C API to load and execute an script file called script.lua.
This script will be in different location when built vs when installed/packed, it's location would be:
[build] : ${CMAKE_CURRENT_SOURCE_DIR}/src/scripts
[install]: ../scripts (relative to app which is in bin directory)
What I'm trying to achieve here is to have install step regenerate configure_file then rebuild using new configure_file and only then proceed to do the normal install step and of course revert the configure_file back to it's original state afterwards.
Any help regarding this issue is appreciated.
My understanding is that CMake's configure_file command has its full effect during the execution of the cmake program. It has no representation in generated makefiles, or whatever other build system components cmake generates. Thus, if you want to configure a file differently for installation than for pre-installation testing,
You would need to perform completely separate builds (starting with executing cmake) for the two cases, and
You would need to use some attribute of the cmake command line or execution environment to convey the wanted information, such as using a -D option to define a CMake variable on the command line.
I advise you not to pursue this route. Aside from being overcomplicated, it's also poor form to install a different build of the software than is tested.
You have a variety of alternatives that could serve better. Among those are
Give the program itself the ability to accept a custom location for the Lua script. That is, make it recognize a command-line argument or environment variable that serves this purpose. Make use of that during pre-installation testing.
If indeed the program is using a relative path to locate the script at runtime, then just (have CMake) put a copy of the script at the appropriate location in the build tree, so that the program will find it normally during testing.

Make clangd aware of macros given from the compiler

I have two executables that are build from the same source (a client and a server) and they're built with the compile options -D CLIENT=0 -D SERVER=1 for the server and -D CLIENT=1 -D SERVER=0 for the client. If I do something like
if (CLIENT) {
// Client specific code
}
clangd complains that CLIENT is not defined. Is there a way to make clangd aware of those macros? (The code compiles just fine, the errors are from clangd, not the compiler)
Is there a way to make clangd aware of those macros?
From getting started with clangd:
Project setup
To understand source code in your project, clangd needs to know the
build flags. (This is just a fact of life in C++, source files are not
self-contained.)
By default, clangd will assume that source code is built as clang
some_file.cc, and you’ll probably get spurious errors about missing
#included files, etc. There are a couple of ways to fix this.
compile_commands.json
compile_commands.json file provides compile commands for all source
files in the project. This file is usually generated by the build
system, or tools integrated with the build system. Clangd will look
for this file in the parent directories of the files you edit. Other
tools can also generate this file. See the compile_commands.json
specification.
compile_commands.json is typically generated with CMake build system, but more build systems try to generate it.
I would suggest moving your project to CMake, in the process you will learn this tool that will definitely help you in further C-ish development.
compile_flags.txt
If all files in a project use the same build flags, you can put those
flags, one flag per line, in compile_flags.txt in your source root.
Clangd will assume the compile command is clang $FLAGS some_file.cc.
Creating this file by hand is a reasonable place to start if your
project is quite simple.
If not moving to cmake, create a compile_flags.txt file with the content for example like the following, and clangd should pick this file up:
-DCLIENT=1
-DSERVER=1

CMake: Header files cannot be opened

I am working to build a Code Composer Studio project using cmake, which is new to me. It builds successfully under Linux but I am struggling to get it to work under Windows. The cmake command executes without issue, but make fails during the very first C object at the very first #include with the error code
fatal error: could not open source file "stdbool.h" (no directories in search list)
I'm using the libraries included in CCS's compiler (c6000_7.4.15), and that whole folder is included in the CSS project. I include it in cmake as well. In my .cmake file:
set (CCS_ROOT ${CCS_ROOT_V6_WIN} CACHE PATH "code composer install directory")
set(CGT_COMPILER_ROOT ${CCS_ROOT}/tools/compiler/c6000_7.4.15 CACHE INTERNAL "DSP Compiler Root")`
And in the CMakeLists.txt file:
set (COMPILER_INCLUDE ${CGT_COMPILER_ROOT}/include)
INCLUDE_DIRECTORIES ("${COMPILER_INCLUDE}")
Why can the header files not be opened when they're linked in the project and CMake can find them just fine?
EDIT: The directory structure had been changed underneath me, so I took the opportunity to add all of the external files directly into the project to make it completely platform-independent. That way, since the project is managed by our Git repository, users won't have to install the CSL or any other programs to build the project. This also means that paths to libraries and header files will never change between revisions and environments.
Unfortunately, this has not solved my problem. The project continues to build in Linux while failing to ind the very first included header file. I also notice that, under Windows, it cannot find my own header files unless I provide a relative path, e.g. #include "../Common.h" I can get make to find stdbool.h if I provide an absolute path to the compiler directory, but that exposes a web of additional broken links between files.
As a side note, the project builds successfully within Code Composer Studio, so I am assuming that this isn't an issue with my specific Windows environment nor with the code within the project itself.
This seems to be an issue with gcc.exe. I set an environment variable CC to the path of a different compiler (in my case a TI compiler) within my build script and that fixed the problem.

How to use libnet with cmake (and kdevelop)?

I am afraid the question I have might be stupid, but as I am new to kdevelop and cmake it is quite hard for me to understand how they work.
The project I tried to set up uses the libnet 1.1 library. My Question is how do I get cmake to compile and link this library so I can use it properly?
Here is what I already tried:
PROJECT(test)
include_directories("${PROJECT_SOURCE_DIR}/libnet")
add_subdirectory(libnet)
ADD_EXECUTABLE(test main.c)
target_link_libraries(test libnet)
Thank you for your Help!
It looks like libnet does not use CMake itself, so you're going to have to build it separately or make it part of your own project.
To build it separately, you have a couple of choices. You can build it (and install it if you want) and then use find_library to locate the actual libnet.a / libnet.lib file.
find_library(libnet NAMES net libnet PATHS <wherever you built it to>)
include_directories(<wherever you built it to>/include)
target_link_libraries(test libnet)
CMake provides a decent way to automate this through use of ExternalProject_Add. This is a little trickier to use, but you can make it download, extract, build and install libnet all in one command. It looks like libnet has several different ways of being built though, depending on platform, so this may not be too straightforward.
Another option would be to include the libnet sources in your own project and add it as a library via add_library. You'd need to create a list of the libnet sources, and also examine the libnet makefiles to check for any compiler flags / oddities that would need special handling in your own CMakeLists.txt
This is perhaps the best option since it gives you access to the full libnet source tree in your IDE, allows you to fine-tune the libnet build, and causes your own project to go out of date (need rebuilding) if the libnet build changes.
set(LibnetSources <list all sources and headers>)
add_library(libnet ${LibnetSources})
include_directories(${PROJECT_SOURCE_DIR}/libnet/include)
target_link_libraries(test libnet)
You can make use of file(GLOB...) to help with generating the list of libnet sources, but it's not recommended since the addition or removal of a file would not be automatically detected by CMake. You need to make sure that if you do this, you re-run cmake manually before trying to recompile. This isn't an issue if you're not planning on adding/deleting any libnet files.
Edit: Use ExternalProject Module
OK, there is a third option which is maybe the best, but can be slightly complex to set up; use CMake's ExternalProject Module. This is designed to allow building of external dependencies - even ones which don't use CMake. This is a decent article on using it.
Try replacing your CMakeLists.txt with this (only tested on Ubuntu with gcc). In short, it downloads libnet, configures it, builds it and installs it to your build tree (not to /usr/local). Your executable can then include and link to it.
# Minimum version 2.8.5 since we need ExternalProject module
cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
project(test)
# Enable ExternalProject CMake module
include(ExternalProject)
# Set default ExternalProject root directory
set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/ThirdParty)
# Add libnet
ExternalProject_Add(
libnet
URL http://packetfactory.openwall.net/libnet/dist/libnet.tar.gz
TIMEOUT 30
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
BUILD_IN_SOURCE ON
# Wrap download, configure, build and install steps in a script to log output
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON)
# Specify include dir
ExternalProject_Get_Property(libnet install_dir)
include_directories(${install_dir}/include)
# Add test executable target
add_executable(test main.c)
# Create dependency of test on libnet
add_dependencies(test libnet)
# Specify test's link libraries
target_link_libraries(test ${install_dir}/lib/libnet.a)

Resources