[CMake] Way to use precompilation directive on cmake and conditionnal linking

Tyler Roscoe tyler at cryptio.net
Tue Mar 24 11:24:12 EDT 2009


On Tue, Mar 24, 2009 at 09:18:01AM +0100, Nadir SOUALEM wrote:
> I would like to use precompilation directive such NO_OPENGL
> Is there a way to use it with this command line ?
> cmake -DCMAKE_BUILD_TYPE=Debug
> 
> or must i use something like:
> cmake -DCMAKE_BUILD_TYPE=Debug -DWITH_OPENGL=0
> 
> with an if statement:
> if(NOT WITH_OPENGL)
> ADD_DEFINITIONS(-DNO_OPENGL)
> endif

I'm not sure I understand this question, but that solution looks
reasonable. Maybe what you want is more like:

if (WITH_OPENGL)
    add_definitions (-DOPENGL)
else (WITH_OPENGL)
    add_definitions (-DNO_OPENGL)
endif (WITH_OPENGL)

> How to make a conditional linking, for example if NO_OPENGL
> 
> 
> TARGET_LINK_LIBRARIES(MyExe
> A
> B
> C)
> 
> else
> 
> TARGET_LINK_LIBRARIES(MyExe
> A
> B
> C
> GL
> GLU
> GLUT)

In my projects I do something like:

set (MyExe_LINK_LIBS
    A
    B
    C)

if (WITH_OPENGL)
    set (MyExe_LINK_LIBS
        ${MyExe_LINK_LIBS}
        GL
        GLU
        GLUT)
endif (WITH_OPENGL)

target_link_libraries(MyExe ${MyExe_LINK_LIBS})


hth,
tyler


More information about the CMake mailing list