我目前正试图从Windows10迁移到Linux(Pop!_OS),但我无法让我的C++项目在后者上正确编译和运行.我的C++项目是使用Visual Studio创建的,我还在其中指定了解决方案属性中的包含文件夹、库文件夹、应该链接的内容等.我现在想改用Neovim编写代码,而不是使用Visual Studio(或Visual Studio代码),并try 通过G++编译它.我很快注意到我的包含文件无法识别,所以我try 使用CMake并创建了一个CMakeLists.txt.我两个都试过了

INCLUDE_DIRECTORIES()

TARGET_INCLUDE_DIRECTORIES()

但无论我输入什么路径,我包含的文件都无法识别.即使我使用了导致第一个错误的特定包含文件的路径,它仍然无法识别.

My goal would be that I can specify an include folder 和 a library folder, so that I can just add files 和 folders in these 和 that the new files 和 folders automatically get recognized when compiling (i.e I would not have to edit the CMakeLists.txt in the future). Is that even possible with CMake 和 if yes, does anyone know where i can find further information about that or does anyone have a CMakeLists.txt file that does this? If no, would I have to specify each 和 every file 和 folder in the CMakeLists.txt file 和 do the same for every new include 和 library?

项目 struct :

Overall folder
\- build
\- include
---> includeFolder1
---> includeFolder2
---> ...
\- libs
---> library1.lib
---> library2.lib
---> ...
\- src
--> main.cpp
--> other .cpp's 和 .h's
--> other folders with .cpp's 和 .h's

I've tried compiling with G++ 和 CMake, but both did not work, no matter what I specified as the include 和 library paths.

推荐答案

我已经找到了导致我犯错的问题.问题不在于CMake,而在于Windows和Linux的特定细节.我总是收到"<;foo\foo.h>;没有这样的文件或目录"这样的错误,这让我认为CMake找不到包含目录或其中的文件.然而,问题出在包含路径本身.在Windows上,路径可以用反斜杠(‘\’)表示,但在Linux上,路径用正斜杠(‘/’)表示.因此,在我的示例中,文件的路径是"../foo/foo.h",但我的代码是"#Include<;foo\foo.h>;".因此,在将项目从Windows迁移到Linux时,一定要注意#INCLUDE语句中的反斜杠!

下面是一个模板CMakeLists.txt,如果您想要将您的Visual Studio项目迁移到Linux,这应该是一个很好的起点.我使用了glfw(+GREAY)作为示例库:

cmake_minimum_required(VERSION 3.20)

project(ExampleProject)

add_executable(${PROJECT_NAME} src/glad.c src/main.cpp)

target_include_directories(${PROJECT_NAME} PRIVATE include)

target_link_libraries(${PROJECT_NAME} GL dl glfw)

Linux相关问答推荐

使用sed替换字符,但如果它是在bash csv文件中的字符串中,则不使用

通过shell 脚本基于AM/PM对日期时间字段进行排序

在linux中,我想将昨天的文件与今天的文件进行比较,仅将今天的更改作为输出,忽略一些字段

如何正确Forking 并完成进程以避免 EAGAIN 错误

jinja2.exceptions.TemplateSyntaxError:预期标记,,得到整数(支持十六进制,八进制和二进制整数文字)

如何在脚本中使用sudo修改其他用户的crontab

如何过滤 Bash 的正则表达式(Linux)中的所有值,除了一个?

Linux 系统调用文档

Docker 容器立即退出

查看 linux 上的多核或多 CPU 利用率

bash 中的sed命令

对一行的最后一个字段进行排序

如何在不重新打印的情况下更新终端中的打印消息

使用inotify的正确方法是什么?

如何在字符串中查找子字符串(或如何 grep 变量)?

C++:使用 longjmp 和 setjmp 安全吗?

如何在shell的递归目录中列出特定类型的文件?

sed - 如何使用 sed 进行正则表达式组

`os.symlink` 与 `ln -s`

在 Docker 容器中运行的 JVM 的驻留集大小 (RSS) 和 Java 总提交内存 (NMT) 之间的差异