简体   繁体   中英

Intellisense not working with CMake FetchContent

I am new to CMake and have been playing around with a "Modern CMake" approach. A brief overview of the project structure:

+ project folder
    - CMakeList
    + src
        - CMakeList
        - lib.cpp
    + tests
        - CMakeList
        - test.cpp

I have two issues that I would like to address. The main one is getting Intellisense to show code option and colorize the text within test.cpp.

my tests CMakeList

# CMakeList for the tests folder

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
)
FetchContent_MakeAvailable(googletest)

# this helps remove the MSVCRTD warning
add_definitions(-DNODEFAULTLIB:MSVCRTD)

# this helps remove a bunch of static vs dynamic erros
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")

add_executable(set1_solu_tests tests.cpp)
target_link_libraries(set1_solu_tests GTest::gtest GTest::gtest_main set1_solu_lib)
add_test(NAME TwoSumTests COMMAND set1_solu_tests)

Everything compiles and can run, however, Intellisense does not work only in test.cpp. Even std::vector is not colorized.

code within google test not colorized:

TEST(TwoSum, set1)
{
    int t = 17;
    std::vector<int> nums{ 2, 3, 4, 5, 7, 9, 11, 12, 17, 16, 20, 10 };
    auto result = twoSum(nums, t);

    std::vector<int> a{ 4, 11 };
    std::vector<int> b{ 11, 4 };
    EXPECT_TRUE(result == a || result == b);
}

I would like to keep it short if possible and add external project seem very long and complicated.

It is quite strange and in my side, the vector is colored by your provided code.

在此处输入图像描述

So please make sure that you have added add_subdirectory ("CMakeList") under CMakeLists.txt file. And make sure that there is no error for your project.

Or you could try this:

One) disable any third party installed extensions under Extensions --> Manage Extensions . When you finish this, you should restart VS.

Two) close VS, delete .vs hidden folder under the solution folder, also the out folder.

Then, restart your VS, open your CMake project to test again.

Besides , if this does not work, please check this:

First, delete the content of your tests CMakeList from CMakeLists.txt file, rebuild cmake project. Then, close VS, try the above step two option. After that, restart VS, your cmake project, re-add the content of your tests CMakeList into CMakeLists.txt file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM