25

I'm using CMake for a project that comes in two versions, one of which requires -lglapi and the other does not.

So far the lines we used look like that:

SET(CMAKE_C_FLAGS "-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm")
SET(CMAKE_CXX_FLAGS "-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm")

I added an if statement in my CMakeList.txt exactly after those lines:

if(SINGLE_MODE)
    SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} " -lglapi")
    SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} " -lglapi")
endif(SINGLE_MODE)

The SINGLE_MODE variable is defined a little up. When I use the message command to display the content of the flag variables it looks alright:

-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm -lglapi

But when I start compiling I am running into a compile error. Using the verbose mode I realized that in the compiler call it looks like that:

-O3 -xSSE3 -restrict -lpthread -lX11 -ldrm; -lglapi

I.e. somehow a semicolon got added before adding the -lglapi to the list.

Did anyone here encounter a similar issue and knows a way to fix this issue? I've googled quite a while and studied the CMake manual but couldn't see what I did wrong here.

Thanks, Tobias

jpo38
  • 20,821
  • 10
  • 70
  • 151
TobiSF
  • 253
  • 1
  • 3
  • 5
  • 5
    Did you try `SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lglapi")`? – jpo38 Apr 27 '15 at 16:42
  • @jpo38 You should make an answer and get your deserved reputation :) – Antonio Apr 27 '15 at 16:46
  • @jpo38, thank you. That did the trick. Should have thought of that myself but after looking at something for too long, you kinda get blind for that. – TobiSF Apr 27 '15 at 16:47
  • Thanks. As I have no CMake installed on my current computer, I could not test. Prefered to have your feedback before posting an untested answer to avoid getting any downvotes....;-). Now ready for upvotes! – jpo38 Apr 27 '15 at 16:50

3 Answers3

34

Try to do this instead:

if(SINGLE_MODE)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lglapi")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lglapi")
endif(SINGLE_MODE)

Then, you are sure you append -lglapi to the existing ${CMAKE_CXX_FLAGS} string. Else, looks like something like a CMake list is being created.

jpo38
  • 20,821
  • 10
  • 70
  • 151
19

Since CMake 3.4 you do:

string(APPEND CMAKE_CXX_FLAGS " -lglapi")

This very handy when you want to set the flags only for one language (C++ in the example above), but if you want to set the same flags for all languages, you can simply do:

add_compile_options(-lglapi)

Both commands change the flags for the whole directory, if you want to set the flags for only one target, do:

target_compile_options(my_lib PUBLIC -lglapi)

Flags on a target can either be PUBLIC, PRIVATE or INTERFACE, allowing to transitively forward the flags from one target to the other.

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
0

Just for the particular case where you want to add compiler and linker options (like for --coverage option), the following syntax add the flags to both the linker and compiler:

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")

(above commands add --coverage option to both compiler and flags options for C and C++)

Otherwise, if you prefer to use new CMake commands (add_compile_options and add_link_options), don't forget to add to the linker option too:

add_compile_options(--coverage)
add_link_options(--coverage)

(above commands add --coverage option to both compiler and flags options for C and C++)

quent
  • 1,936
  • 1
  • 23
  • 28