英文:
How to generate a multi-file godbolt project from scratch?
问题
从reddit帖子中,我找到了https://godbolt.org/z/WseTsM8YG,并决定从头开始在godbolt上尝试制作一个多文件项目。但是,似乎出了点问题。我的项目:https://godbolt.org/z/7asGz3Wov
似乎main.cpp
无法看到header.h
文件,原因不明。我分别添加了每个文件,它们似乎在同一个目录中,如下所示:
我甚至尝试将当前目录添加到包含列表中(这应该是不必要的)使用-I.
命令行开关。不出所料,它没有起作用。
我的原始CMakeLists.txt
文件:
cmake_minimum_required(VERSION 3.5)
project(main.exe VERSION 1.0 LANGUAGES CXX)
add_executable(main main.cpp source1.cpp source2.cpp)
然后,我改成了与reddit示例对齐:
cmake_minimum_required(VERSION 3.5)
#project(main.exe VERSION 1.0 LANGUAGES CXX)
add_executable(main main.cpp source1.cpp source2.cpp)
target_link_libraries(main)
这也没有起作用。
看起来文件可能不在同一个目录中。我错过了什么魔法来实现这一点?
英文:
So from the reddit post, I got to https://godbolt.org/z/WseTsM8YG and I decided I'd try my hand at making a multi-file project on godbolt from scratch. However, something seems to be wrong. My project: https://godbolt.org/z/7asGz3Wov
Seems that main.cpp
can't see the header.h
file for some reason. I added each file separately, and they appear to be in the same directory as shown here:
I even attempted to add the current directory to the include list (which shouldn't be necessary) with the -I.
command line switch. Not surprisingly, it didn't work.
My original CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.5)
project(main.exe VERSION 1.0 LANGUAGES CXX)
add_executable(main main.cpp source1.cpp source2.cpp)
I then changed to align with the reddit example:
cmake_minimum_required(VERSION 3.5)
#project(main.exe VERSION 1.0 LANGUAGES CXX)
add_executable(main main.cpp source1.cpp source2.cpp)
target_link_libraries(main)
That didn't do anything either.
It would appear the files are not located in the same directory. What magic am I missing to do that?
答案1
得分: 1
你现在打开的编译选项卡不使用你的多文件 CMake 设置。
在 Tree View 中,你需要点击 "添加新的",然后选择 "仅执行"。这将触发使用给定的 CMake 选项执行 cmake --build
。它还会告诉你其他仍需要修复的问题(你的代码无法编译)。
此外,你的 target_link_libraries
没有任何作用,因为你没有指定要链接的库。
请参考这里:godbolt.org/z/1Woeo6x9E
英文:
The compiler tab you have open does not use your Multifile CMake setup.
In the Tree View, you need to click on "Add new" and then "Execution only". This will trigger the execution of cmake --build
with the given cmake options. It will also tell you all the other things that still need fixing (your code doesn't compile).
In addition, your target_link_libraries does nothing, since you don't specify any libraries to link to.
See here: godbolt.org/z/1Woeo6x9E
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论