如何从零开始生成一个多文件的Godbolt项目?

huangapple go评论68阅读模式
英文:

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文件,原因不明。我分别添加了每个文件,它们似乎在同一个目录中,如下所示:
如何从零开始生成一个多文件的Godbolt项目?

我甚至尝试将当前目录添加到包含列表中(这应该是不必要的)使用-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:
如何从零开始生成一个多文件的Godbolt项目?

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

huangapple
  • 本文由 发表于 2023年2月24日 06:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定