混合 C 和 C++使用CMAKE

发布于 2024-12-15 04:23:25 字数 173 浏览 4 评论 0原文

我们主要用 C 编写应用程序,但一些子模块是用 C++ 编写的(在 Linux 上)。问题是如何编写 CMakeLists.txt 文件,以便对某些子目录使用 g++ ,而对另一个子目录使用 gcc

We write an application mainly in C but some sub-modules are written in C++ (on Linux). The problem is how to write CMakeLists.txt files to use g++ for some subdirectories and gcc for another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别闹i 2024-12-22 04:23:25

如果没有另外设置,编译器和链接器通常由文件扩展名决定。因此,只要文件结尾正确,您的代码就会被编译并与正确的编译器链接。

附带说明一下,如果混合使用 C 和 C++,请记住进行正确的 extern C 声明。

The compiler and linker is usually determined by the file extension if not set otherwise. So as long as the file endings are fine, your code is compiled and linked with the correct compiler.

On a side note, remember to make the correct extern C declarations, if you mix C and C++.

何处潇湘 2024-12-22 04:23:25

CMake 应利用文件扩展名指定的相关编译器和链接器。在使用 C 和 C++ 组合编译并链接到可执行文件的实例中,您可能会说:

add_executable(MyEXE main.cpp myFile.c)

我想添加一个非常琐碎的点。

在这种情况下,您必须确保您的项目具有正确的语言名称参数,即 1

project(MyProject C CXX)

这应该在添加 CMake 文件时自动完成,但根据您添加文件的顺序,参数可能只包含 C 或 CXX。

例如,如果您仅将 C 作为语言参数,并且您的 int main() 位于 main.cpp 中,则您的程序入口点将不存在,并且编译器将忽略错误。


1 或者,您可以省略语言参数,在这种情况下,将选择 C ​​和 CXX 的默认参数。请参阅此处

CMake should utilise the relevant compiler and linker specified by the file extensions. In the instance where you are compiling and linking into an executable using a combination of C and C++, you would say:

add_executable(MyEXE main.cpp myFile.c)

I'd like to add a painfully trivial point.

In this instance, you must ensure that your project has the correct language name arguments, namely 1:

project(MyProject C CXX)

This should be done automatically when adding a CMake file, but depending on the order you added the files, the arguments might only include C or CXX.

If you only had for instance C as a language argument and your int main() was in your main.cpp, your program entry point wont exist and your compiler will omit an error.


1 Or you may omit the language arguments in which case the default arguments of C and CXX is selected. See here

从﹋此江山别 2024-12-22 04:23:25

CMake 自动执行此操作。您可以在 CMakeLists.txt 文件中自由混合两种类型的文件:

. . .
add_executable(
    my_program
    code.cpp
    more_code.c
)

我一直这样做,而且它确实有效。

CMake does this automatically. You can freely intermix both types of files in your CMakeLists.txt file:

. . .
add_executable(
    my_program
    code.cpp
    more_code.c
)

I do this all the time and it just works.

清风无影 2024-12-22 04:23:25

您可以将源文件的 LANGUAGE 属性设置为“CXX”。请参阅文档

You can set LANGUAGE property of your source files to "CXX". See documentation.

一萌ing 2024-12-22 04:23:25

g++ 和 gcc 之间的区别基本上是 g++ 将 -lstdc++ 传递给链接器。只需添加 C++ 标准库作为 C++ 模块的显式依赖项即可。

需要明确的是,gcc 可以编译 C++ 代码。 gcc 和 g++ 在这方面是相同的。区别仅在于,使用 g++ 时,您不必显式告诉编译器链接到 libstdc++。

The difference between g++ and gcc is basically that g++ passes -lstdc++ to the linker. Just add the c++ standard library as an explicit dependency of the c++ modules.

To be clear, gcc can compile C++ code. gcc and g++ are the same in this regard. The difference is only that when using g++ you don't have to explicitly tell the compiler to link to libstdc++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文