Faded code under #ifdef when linking with library
已回答
I have a some files i compile, a.cpp, b.cpp, c.cpp with some of the code under #ifdef XX.
When i use
add_executable(my_exec a.cpp b.cpp c.cpp)
target_compile_definitions(my_exec PRIVATE XX)
And look at the files in CLion i see the code under #ifdef XX as valid.
But when i use
add_library(my_lib STATIC a.cpp b.cpp)
target_compile_definitions(my_lib PUBLIC XX)
add_executable(my_exec c.cpp)
target_link_libraries(my_exec my_lib)
I see the code in c.cpp under #ifdef XX as valid but in a.cpp and b.cpp it is faded and the #else part is valid.
Am i missing some CMake definition or is this a bug?
请先登录再写评论。
Hello! Unfortunately, I can't reproduce the described situation: I see code under #ifdef as valid in all files. What CLion version do you use? Could you please provide a simple sample project with which the issue can be reproduced to clion-support at jetbrains.com (just to exclude the possibility that I'm missing something)? Thanks in advance!
I am using 2018.2 EAP build #CL-182.3458.13
The simple example is:
cmake_minimum_required(VERSION 3.11)
project(ifdef_example)
set(CMAKE_CXX_STANDARD 11)
add_library(my_a_lib STATIC a.cpp)
target_compile_definitions(my_a_lib PUBLIC MY_MACRO)
add_library(_my_bad_lib STATIC a.cpp)
target_compile_definitions(_my_bad_lib PUBLIC BAD_MACRO)
add_executable(ifdef_example main.cpp)
target_link_libraries(ifdef_example my_a_lib)
The issue happens when the second library starts with "_" in its name.
Is it illegal?
So, according to your CMakeLIsts.txt example, there is the a.cpp file which is added to two different targets (my_a_lib and _my_bad_lib) with different flags. In this case the way how the code in a.cpp is highlighted depends on the resolve context. You can change it by clicking the Context field of the Status bar.
If my_a_lib is selected, MY_MACRO is defined and BAD_MACRO is not, so the code is highlighted accordingly:

If _my_bad_lib is selected, BAD_MACRO is defined and MY_MACRO is not, so the code is highlighted accordingly:
As for the main.cpp: it's added to one target (ifdef_example) which is linked to my_a_lib, so for this file there is only one resolve context in which MY_MACRO is defined and BAD_MACRO is not:
I understand, thank you.