Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - how to set multiple compile definitions for target executable?

Tags:

cmake

I'm trying to set multiple compile definitions for one of the executables I'm trying to compile in CMake (in order to activate macros used for debugging). Here's what I tried:

add_executable (trie_io_test trie_io_test.c trie.c word_list.c)     set_target_properties(             trie_io_test             PROPERTIES             COMPILE_DEFINITIONS UNIT_TESTING=1)     set_target_properties(             trie_io_test             PROPERTIES             COMPILE_DEFINITIONS IO_TEST=1) 

Unfortunately this causes only the IO_TEST to be defined.

I also tried the following:

add_executable (trie_io_test trie_io_test.c trie.c word_list.c)     set_target_properties(             trie_io_test             PROPERTIES             COMPILE_DEFINITIONS UNIT_TESTING=1 IO_TEST=1) 

But this, on the other hand, causes CMake error.

How to set both of these definitions for the executable I'm trying to build?

like image 393
qiubit Avatar asked May 30 '15 13:05

qiubit


People also ask

How do you add a definition on CMake?

Add -D define flags to the compilation of source files. Adds definitions to the compiler command line for targets in the current directory, whether added before or after this command is invoked, and for the ones in sub-directories added after.

What is Target_compile_definitions?

Specifies compile definitions to use when compiling a given <target> . The named <target> must have been created by a command such as add_executable() or add_library() and must not be an ALIAS target. The INTERFACE , PUBLIC and PRIVATE keywords are required to specify the scope of the following arguments.

What are targets in CMake?

A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.

Can you use CMake with C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).


2 Answers

You want target_compile_definitions instead of set_target_properties:

target_compile_definitions(trie_io_test PRIVATE UNIT_TESTING=1 IO_TEST=1) 
like image 156
Fraser Avatar answered Oct 03 '22 22:10

Fraser


I find this can work for you:

add_executable (trie_io_test trie_io_test.c trie.c word_list.c) set_target_properties(     trie_io_test     PROPERTIES     COMPILE_DEFINITIONS UNIT_TESTING=1     COMPILE_DEFINITIONS IO_TEST=1    ) 

Just by adding another COMPILE_DEFINITIONS :P

like image 26
Chen Avatar answered Oct 03 '22 23:10

Chen