Help us understand the problem. What is going on with this article?

cmakeでCMAKE_C_FLAGSに設定した値が無視される

More than 1 year has passed since last update.

CMakeLists.txtの中でコンパイルオプションを指定しているつもりなのに、設定が無視される問題に遭遇しました。
コンパイルオプションの指定と、add_subdirectoryコマンドの記載順序の問題でした。

問題

CMakeLists.txtの抜粋
cmake_minimum_required(VERSION 3.12)
project(cmake_01)
set(APP_NAME example)
add_subdirectory(src)

# これらが無視される。なぜ?
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0")

set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})

make実行(-gしか指定されていない。無視されている?)
/usr/bin/c++   -I../include  -g   -o main.cpp.o -c main.cpp
/usr/bin/cc  -I../include  -g   -o target.c.o   -c target.c

解決

add_subdirectoryコマンドを呼ぶ前にCMAKE_C_FLGASらをセットしておく必要があったみたい。

CMakeLists.txtの抜粋
cmake_minimum_required(VERSION 3.12)
project(cmake_01)
set(APP_NAME example)

# これらが無視される。なぜ?
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0")

set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})

# add_subdirectoryコマンドの呼出場所が拙かった。
add_subdirectory(src)
make実行(思ったとおりのオプションになった)
/usr/bin/c++   -I../include    -Wall -g -g -O0   -o main.cpp.o -c main.cpp
/usr/bin/cc  -I../include    -Wall -g -g -O0   -o target.c.o   -c target.c

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away