TheLartians / ModernCppStarter

🚀 Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.
https://thelartians.github.io/ModernCppStarter
The Unlicense
4.33k stars 381 forks source link

Unable to open source file "doctest/doctest.h" and "cxxopts.hpp" #176

Closed ENate closed 1 year ago

ENate commented 1 year ago

I clone the repo and opened it using CLION and visual studio code. I expected that both the doctest.hpp and cxxopts.hpp will be added automatically (since both packages are available in the CMakeLists.txt. But this is not the case as I get the following errors:

cannot open source file "doctest/doctest.h"     
cannot open source file "cxxopts/cxxopts.h"
ClausKlein commented 1 year ago

Which project dit you open?

./CMakeLists.txt
./all/CMakeLists.txt
./documentation/CMakeLists.txt
./standalone/CMakeLists.txt
./test/CMakeLists.txt

I guess all.

What is your environment settings and the output of cmake config step?

i.e.:

rm -rf build/
bash-3.2$ cmake -S all -B build/all -G Ninja
-- The CXX compiler identification is AppleClang 14.0.3.14030022
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: /usr/local/bin/git (found version "2.41.0") 
-- CPM: Adding package cxxopts@3.0.0 (v3.0.0 at /Users/clausklein/.cache/CPM/cxxopts/0c1df694f5ab3306e541038e6a2ed62926062894)
-- CPM: Adding package Greeter@ (/Users/clausklein/Workspace/cpp/ModernCppStarter/standalone/..)
-- CPM: Greeter: Adding package PackageProject.cmake@1.8.0 (v1.8.0 at /Users/clausklein/.cache/CPM/packageproject.cmake/987b02f8a9fe04de3c43e0e7a1afbb29c87adc5e)
-- CPM: Greeter: Using local package fmt@10.0.0
-- CPM: Using local package doctest@2.4.11
-- Found Python: /usr/local/Frameworks/Python.framework/Versions/3.11/bin/python3.11 (found version "3.11.4") found components: Interpreter 
-- CPM: Adding package Format.cmake@1.7.3 (v1.7.3 at /Users/clausklein/.cache/CPM/format.cmake/17e103764947115e78d95ecc29c4bee54dc64e08)
CMake Error at /Users/clausklein/Workspace/cpp/ModernCppStarter/test/CMakeLists.txt:52 (include):
  include could not find requested file:

    /scripts/cmake/doctest.cmake

CMake Error at /Users/clausklein/Workspace/cpp/ModernCppStarter/test/CMakeLists.txt:53 (doctest_discover_tests):
  Unknown CMake command "doctest_discover_tests".

-- Configuring incomplete, errors occurred!
bash-3.2$ 
bash-3.2$ echo $CPM_SOURCE_CACHE       
/Users/clausklein/.cache/CPM
bash-3.2$ echo $CPM_USE_LOCAL_PACKAGES 
yes
bash-3.2$ 

This would be fixed with https://github.com/TheLartians/ModernCppStarter/pull/177

ClausKlein commented 1 year ago

with the right setting, I get this:

bash-3.2$ cd build/all/
bash-3.2$ ninja 
[0/2] Re-checking globbed directories...
[10/10] Linking CXX executable test/GreeterTests
bash-3.2$ ninja -t deps | grep -vw usr | grep -vw Xcode | sort -u 

    /Users/clausklein/.cache/CPM/cxxopts/0c1df694f5ab3306e541038e6a2ed62926062894/include/cxxopts.hpp
    /Users/clausklein/.cache/CPM/doctest/bbb3e61c4790cca23f5279ff9fddd6ec56ef3bd4/doctest/doctest.h
ENate commented 1 year ago

Running these commands:

rm -rf build/
cmake -S all -B build/all -G Ninja
cd build/all/
ninja 

I got the following:

Building CXX object _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o
FAILED: _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o 
/usr/bin/c++  -I/home/development/cppMonitor/include -I/home/development/cppMonitor/build/all/_deps/greeter-build/PackageProjectInclude -I/home/development/cppMonitor/build/all/_deps/fmt-src/include -Wall -Wpedantic -Wextra -Werror -MD -MT _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o -MF _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o.d -o _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o -c /home/development/cppMonitor/standalone/source/main.cpp
/home/development/cppMonitor/standalone/source/main.cpp:4:10: fatal error: cxxopts.hpp: No such file or directory
    4 | #include <cxxopts.hpp>
      |          ^~~~~~~~~~~~~
compilation terminated.
[7/11] Building CXX object standalone/CMakeFiles/GreeterStandalone.dir/source/main.cpp.o
ninja: build stopped: subcommand failed.
ClausKlein commented 1 year ago

I need the output of cmake. Which cmake version are you using?

ENate commented 1 year ago

cmake version 3.26.4 Do you mean the output I got by running

cmake -S all -B build
cmake --build  build
ClausKlein commented 1 year ago

The output of cmake -S all -B build would help.

But it seems you need this patch: https://github.com/ClausKlein/ModernCppStarter/commit/41152efc02a86b53c3ad26c16eafc9068e7805e2

ClausKlein commented 1 year ago

@ENate Can you confirm that my patch works please?

ENate commented 1 year ago

Sorry guys, thanks. I will check please. Just saw your messages.

ENate commented 1 year ago

I changed it to your update @ClausKlein and running this command

cmake -S all -B build

It works well as shown in the following output;

-- The CXX compiler identification is GNU 11.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Downloading CPM.cmake to /home/development/cppProject/build/cmake/CPM_0.37.0.cmake
-- CPM: Adding package cxxopts@3.1.1 (v3.1.1)
-- CPM: Adding package Greeter@ (/home/development/cppProject/standalone/..)
-- CPM: Greeter: Adding package PackageProject.cmake@1.8.0 (v1.8.0)
-- CPM: Greeter: Adding package fmt@9.1.0 (9.1.0)
-- Module support is disabled.
-- Version: 9.1.0
-- Build type: 
-- CXX_STANDARD: 11
-- Performing Test has_std_11_flag
-- Performing Test has_std_11_flag - Success
-- Performing Test has_std_0x_flag
-- Performing Test has_std_0x_flag - Success
-- Required features: cxx_variadic_templates
-- CPM: Adding package doctest@2.4.9 (v2.4.9)
-- CPM: Adding package Format.cmake@1.7.3 (v1.7.3)
-- Found Python: /home/miniconda3/bin/python3.10 (found version "3.10.10") found components: Interpreter 
-- Format.cmake: clang-format and/or python not found, adding dummy targets
-- Format.cmake: cmake-format and/or git not found, adding dummy targets
-- The C compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- CPM: Adding package m.css@0 (a0d292ec311b97fefd21e93cdefb60f88d19ede6)
-- Configuring done (43.7s)
-- Generating done (0.1s)
-- Build files have been written to: /home/development/cppProject/build

The issue is when I run: cmake --build build Then I get get the following error:

[  9%] Building CXX object _deps/fmt-build/CMakeFiles/fmt.dir/src/format.cc.o
[ 18%] Building CXX object _deps/fmt-build/CMakeFiles/fmt.dir/src/os.cc.o
[ 27%] Linking CXX static library libfmt.a
[ 27%] Built target fmt
[ 36%] Building CXX object _deps/greeter-build/CMakeFiles/Greeter.dir/source/greeter.cpp.o
[ 45%] Building CXX object _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o
/home/development/cppProject/standalone/source/main.cpp:4:10: fatal error: cxxopts.hpp: No such file or directory
    4 | #include <cxxopts.hpp>
      |          ^~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [_deps/greeter-build/CMakeFiles/Greeter.dir/build.make:90: _deps/greeter-build/CMakeFiles/Greeter.dir/standalone/source/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:284: _deps/greeter-build/CMakeFiles/Greeter.dir/all] Error 2

It clearly shows that I cannot get both cxxopts.hppand doctest.hpp to be picked up by the dev environment.

ClausKlein commented 1 year ago

What is your OS? Which cmake version are you using? Pleas yous ninja -v to compile to see the include paths!

https://github.com/TheLartians/ModernCppStarter/pull/177 works for me in every cases with every compile we use?

ClausKlein commented 1 year ago

I can't understand you problem:

it works on all CI and on my host (OSX)

bash-3.2$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'remotes/origin/master' by 3 commits.
  (use "git push" to publish your local commits)
bash-3.2$ !cmake
cmake -S standalone -G Ninja -B build
-- The CXX compiler identification is AppleClang 14.0.3.14030022
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Downloading CPM.cmake to /Users/clausklein/Workspace/cpp/ModernCppStarter/build/cmake/CPM_0.38.2.cmake
-- CPM: Adding package cxxopts@3.1.1 (v3.1.1)
-- CPM: Adding package Greeter@ (/Users/clausklein/Workspace/cpp/ModernCppStarter/standalone/..)
-- CPM: Greeter: Adding package PackageProject.cmake@1.8.0 (v1.8.0)
-- CPM: Greeter: Adding package fmt@9.1.0 (9.1.0)
-- Module support is disabled.
-- Version: 9.1.0
-- Build type: 
-- CXX_STANDARD: 11
-- Performing Test has_std_11_flag
-- Performing Test has_std_11_flag - Success
-- Performing Test has_std_0x_flag
-- Performing Test has_std_0x_flag - Success
-- Required features: cxx_variadic_templates
-- Performing Test HAS_NULLPTR_WARNING
-- Performing Test HAS_NULLPTR_WARNING - Success
-- Configuring done (11.6s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/clausklein/Workspace/cpp/ModernCppStarter/build
bash-3.2$ ninja -v -C build/
ninja: Entering directory `build/'
[0/2] /usr/local/Cellar/cmake/3.27.1/bin/cmake -P /Users/clausklein/Workspace/cpp/ModernCppStarter/build/CMakeFiles/VerifyGlobs.cmake
[1/7] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/Users/clausklein/Workspace/cpp/ModernCppStarter/include -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/greeter-build/PackageProjectInclude -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/fmt-src/include -std=gnu++17 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -MD -MT _deps/greeter-build/CMakeFiles/Greeter.dir/source/greeter.cpp.o -MF _deps/greeter-build/CMakeFiles/Greeter.dir/source/greeter.cpp.o.d -o _deps/greeter-build/CMakeFiles/Greeter.dir/source/greeter.cpp.o -c /Users/clausklein/Workspace/cpp/ModernCppStarter/source/greeter.cpp
[2/7] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/fmt-src/include -std=gnu++11 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -MD -MT _deps/fmt-build/CMakeFiles/fmt.dir/src/os.cc.o -MF _deps/fmt-build/CMakeFiles/fmt.dir/src/os.cc.o.d -o _deps/fmt-build/CMakeFiles/fmt.dir/src/os.cc.o -c /Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/fmt-src/src/os.cc
[3/7] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/fmt-src/include -std=gnu++11 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -MD -MT _deps/fmt-build/CMakeFiles/fmt.dir/src/format.cc.o -MF _deps/fmt-build/CMakeFiles/fmt.dir/src/format.cc.o.d -o _deps/fmt-build/CMakeFiles/fmt.dir/src/format.cc.o -c /Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/fmt-src/src/format.cc
[4/7] : && /usr/local/Cellar/cmake/3.27.1/bin/cmake -E rm -f _deps/fmt-build/libfmt.a && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc _deps/fmt-build/libfmt.a  _deps/fmt-build/CMakeFiles/fmt.dir/src/format.cc.o _deps/fmt-build/CMakeFiles/fmt.dir/src/os.cc.o && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib _deps/fmt-build/libfmt.a && /usr/local/Cellar/cmake/3.27.1/bin/cmake -E touch _deps/fmt-build/libfmt.a && :
[5/7] : && /usr/local/Cellar/cmake/3.27.1/bin/cmake -E rm -f _deps/greeter-build/libGreeter.a && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc _deps/greeter-build/libGreeter.a  _deps/greeter-build/CMakeFiles/Greeter.dir/source/greeter.cpp.o && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib _deps/greeter-build/libGreeter.a && /usr/local/Cellar/cmake/3.27.1/bin/cmake -E touch _deps/greeter-build/libGreeter.a && :
[6/7] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/Users/clausklein/Workspace/cpp/ModernCppStarter/include -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/greeter-build/PackageProjectInclude -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/cxxopts-src/include -std=gnu++17 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -MD -MT CMakeFiles/GreeterStandalone.dir/source/main.cpp.o -MF CMakeFiles/GreeterStandalone.dir/source/main.cpp.o.d -o CMakeFiles/GreeterStandalone.dir/source/main.cpp.o -c /Users/clausklein/Workspace/cpp/ModernCppStarter/standalone/source/main.cpp
[7/7] : && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/GreeterStandalone.dir/source/main.cpp.o -o Greeter  _deps/greeter-build/libGreeter.a  _deps/fmt-build/libfmt.a && :
bash-3.2$ cd build
bash-3.2$ which c++
/usr/bin/c++
bash-3.2$ c++ --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
bash-3.2$ which g++-13 
/usr/local/bin/g++-13
bash-3.2$ /usr/local/bin/g++-13 \
> -I/Users/clausklein/Workspace/cpp/ModernCppStarter/include \
> -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/greeter-build/PackageProjectInclude \
> -I/Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/cxxopts-src/include \
> -std=gnu++17 -isysroot \
> /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -MD -MT \
> CMakeFiles/GreeterStandalone.dir/source/main.cpp.o -MF CMakeFiles/GreeterStandalone.dir/source/main.cpp.o.d -o \
> CMakeFiles/GreeterStandalone.dir/source/main.cpp.o -c \
> /Users/clausklein/Workspace/cpp/ModernCppStarter/standalone/source/main.cpp

bash-3.2$ 
bash-3.2$ less CMakeFiles/GreeterStandalone.dir/source/main.cpp.o.d
bash-3.2$ grep -w _deps CMakeFiles/GreeterStandalone.dir/source/main.cpp.o.d
 /Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/greeter-build/PackageProjectInclude/greeter/version.h \
 /Users/clausklein/Workspace/cpp/ModernCppStarter/build/_deps/cxxopts-src/include/cxxopts.hpp \
bash-3.2$ 
ENate commented 1 year ago

My cmake version is: 3.22.1 OS: Ubuntu 22.04. I will check with ninja -v and see . Thanks

ClausKlein commented 1 year ago

My cmake version is: 3.22.1 OS: Ubuntu 22.04. I will check with ninja -v and see . Thanks

You may try this: pip install cmake ninja to upgrade cmake ...

ENate commented 1 year ago

I realized that there is an error coming from the following line:

CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.8.0")

Do we need this since I have already created a template from the repo? Thanks. I have not been able to make the IDEs recognize the doctest/doctest.h and cxxopts.hppfollowing the discussions above.

ENate commented 1 year ago

I got the following error trying to build the template again:

CMake Error at /home/miniconda3/lib/python3.10/site-packages/cmake/data/share/cmake-3.26/Modules/FetchContent.cmake:1610 (message):CMake step for packageproject.cmake failed: 1

from the file:

~/project-template/build/cmake/CPM_0.37.0.cmake

regarding issues on lines:

cmake_language(EVAL CODE "
      __FetchContent_directPopulate(
        ${contentNameLower}
        ${quietFlag}
        UPDATE_DISCONNECTED ${disconnectUpdates}
        SUBBUILD_DIR \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-subbuild\"
        SOURCE_DIR   \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-src\"
        BINARY_DIR   \"${FETCHCONTENT_BASE_DIR}/${contentNameLower}-build\"
        # Put the saved details last so they can override any of the
        # the options we set above (this can include SOURCE_DIR or
        # BUILD_DIR)
        ${__detailsQuoted}
      )"
    )

was unable to execute the line:

# PackageProject.cmake will be used to make our target installable
CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.8.0")

inside the ~/project-template/CMakeLists.txt file. I manually added the following file to Vscode and it seem to be picking up the contents of the cxxopts.hpp header file in the IDE:

~/project-template/build/_deps/cxxopts-src/include/cxxopts.hpp

But lose the config again if I close and open the IDE. Strange indeed.

ClausKlein commented 1 year ago

Sorry, I only used VScode once and uninstalled it immediately.

I would remove the your project and clone this ModernCmakeStarter It works without any problems.

ClausKlein commented 1 year ago

~/project-template/build/_deps/cxxopts-src/include/cxxopts.hpp But lose the config again if I close and open the IDE. Strange indeed?

no, build tree is temporär!

ClausKlein commented 1 year ago

Please read the CMake Tutorial first

ENate commented 1 year ago

Surely did :) . I figured out that the IDE was not picking up both cxxopts and doctest.h after explicitly adding them both (../../build/_deps/doctest-src/doctest/doctest.h and #include "../../build/_deps/cxxopts-src/include/cxxopts.hpp") from the build folder. Hence, it seems to be working. Will check out why this is so.

Thanks

ClausKlein commented 1 year ago

FYI: This is related to vscode-cmake-tools/issues/1616 with FetchContent

So CpmAddPackage can't work.

ENate commented 1 year ago

ok Thanks for that!