c - Public flags are not applied to libraries (cmake) -


with cmake, apply general flags both executable , libraries.

well, thought use target_compile_options using public keyword. tested on small example executable , static library, both having 1 file (main.c & mylib.c), not work expected.

the root cmakelists.txt looks this:

cmake_minimum_required(version 3.0)  # add library add_subdirectory(mylib)  # create executable add_executable(mytest main.c)  # link library target_link_libraries(mytest mylib)  # add public flags  target_compile_options(mytest public -wall) 

and library's cmakelists.txt:

cmake_minimum_required(version 3.0)  add_library(mylib static mylib.c) 

the flag -wall applied on main.c , not on library file (mylib.c):

[ 25%] building c object mylib/cmakefiles/mylib.dir/mylib.c.o cd /patsux/programmation/repositories/test-cmake-public/build/mylib && /usr/lib/hardening-wrapper/bin/cc     -o cmakefiles/mylib.dir/mylib.c.o   -c /patsux/programmation/repositories/test-cmake-public/mylib/mylib.c [ 50%] linking c static library libmylib.a [ 25%] building c object cmakefiles/mytest.dir/main.c.o /usr/lib/hardening-wrapper/bin/cc    -wall -o cmakefiles/mytest.dir/main.c.o   -c /patsux/programmation/repositories/test-cmake-public/main.c 

now, if flags applied on library instead of executable, work.

# add public flags on library target_compile_options(mylib public -wall) 

i get:

[ 25%] building c object mylib/cmakefiles/mylib.dir/mylib.c.o cd /patsux/programmation/repositories/test-cmake-public/build/mylib &&        /usr/lib/hardening-wrapper/bin/cc    -wall -o cmakefiles/mylib.dir/mylib.c.o   -c /patsux/programmation/repositories/test-cmake-public/mylib/mylib.c [ 50%] linking c static library libmylib.a  [ 75%] building c object cmakefiles/mytest.dir/main.c.o /usr/lib/hardening-wrapper/bin/cc    -wall -o cmakefiles/mytest.dir/main.c.o   -c /patsux/programmation/repositories/test-cmake-public/main.c [100%] linking c executable mytest 

that makes no sense set general flags such type of target on library.

how can share general flags ? know can use add_definitions(). right way ?

i tested:

set_target_properties(mytest properties compile_flags -wall) 

but flags not public.

you can add flag in root file:

add_compile_options(-wall) 

or, if using cmake version prior 3.0:

set(cmake_cxx_flags "${cmake_cxx_flags} -wall") 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -