简体   繁体   English

我可以指示 g++ 编译扩展名为“.cpp”等的 c++ 源文件吗?

[英]Can I instruct g++ to compile a c++ source file with a different extension than “.cpp” etc?

I am writing a library/framework that can be configured as a CPU-only implementation or a hybrid CPU-GPU implementation.我正在编写一个可以配置为仅 CPU 实现或混合 CPU-GPU 实现的库/框架。 As an example, say I the following files:例如,假设我有以下文件:

LibraryMainCode.cpp
LibraryMainCode.h
Numerics.cpp
Numerics.h

For a CPU-only implementation, the build is straightforward:对于仅 CPU 的实现,构建很简单:

g++ -I. -c Numerics.cpp -o Numerics.o
g++ -I. -c LibraryMainCode.cpp -o LibraryMainCode.o
(linking, etc)

For a hybrid implementation, the build is a little bit more complicated:对于混合实现,构建有点复杂:

g++ -I. -c Numerics.cpp -o Numerics.o
g++ -I. -c LibraryMainCode.cpp -o LibraryMainCode.o
nvcc -I. -x cu -dc LibraryMainCode.cpp -o XLibraryMainCode.o
(linking, etc)

Ie, I am compiling each of these "hybrid" files twice, once as a cpu-only object and once with gpu code as well.即,我将这些“混合”文件中的每一个编译两次,一次作为仅 cpu 的 object 和一次使用 gpu 代码。

However, my codebase is growing quite rapidly and I would like to simply let the file extension dictate how the file is compiled.但是,我的代码库增长非常迅速,我只想让文件扩展名决定文件的编译方式。 For example, I could change the filenames:例如,我可以更改文件名:

LibraryMainCode.cpp
LibraryMainCode.h
Numerics.cppx
Numerics.h

Then, my makefile can simply find these ".cppx" files and compile them accordingly.然后,我的 makefile 可以简单地找到这些“.cppx”文件并进行相应的编译。 Unfortunately, if I write a simple "Hello World" program with extension ".cppx" then g++ will not compile it.不幸的是,如果我编写一个扩展名为“.cppx”的简单“Hello World”程序,那么 g++ 将无法编译它。

I realize that g++ has a list of recognized c++ extensions so I could use comething like ".cxx" for these hybrid files and ".cpp" otherwise, but I would like to stress that the hybrid files are not necessarily standard c++ code and must have special treatment in the makefile.我意识到 g++ 有一个公认的 c++ 扩展名列表,所以我可以对这些混合文件使用“.cxx”之类的东西,否则使用“.cpp”,但我想强调混合文件不一定是标准的 Z6CE809EACF90366 代码并且必须在makefile中有特殊处理。

Is there an easy way to forcefully instruct g++ to compile the source file with an arbitrary extension?有没有一种简单的方法可以强制指示 g++ 编译具有任意扩展名的源文件?

You can explicitly specify the language of a source file using the -x <lang> option:您可以使用-x <lang>选项显式指定源文件的语言:

g++ -x c++ file.strange_extension

you could use a string variable in your makefile which includes the compiler options and use it for every cpp file.您可以在 makefile 中使用字符串变量,其中包括编译器选项并将其用于每个 cpp 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 c++ Autotools 项目中使用不同版本的 g++ 进行编译 - How can I compile with different version of g++ in c++ Autotools project 使用 c++ 命令而不是 g++ (Linux) 编译 .cpp 文件 - Compiling a .cpp file with c++ command instead of g++ (Linux) 用户定义的用于G ++编译的C ++文件扩展 - User defined Extension of C++ file for g++ compilation 我如何用openmp编译C ++? (在Windows 10中使用g ++) - How can i compile c++ with openmp? (using g++ in windows 10) 如何使用 GNU g++ 仅编译标准 C++? - How can I compile only standard C++ with GNU g++? 如何使用根库编译C ++程序包括使用g ++? - How can I compile a C++ program with root library includes using g++? 如何编译C ++项目(与g ++)以便在其他计算机上使用? - How can I compile a C++ project (with g++) to use on other computers? C ++(g ++)编译错误,预期为“ =” /等。 在“ MyWindow”之前(我的班级名称) - C++ (g++) Compile Error, Expected “=”/etc. Before 'MyWindow" (my class name) 尽管使用g ++,clang等进行编译,但C ++链接器错误 - C++ linker error despite using g++, clang etc to compile 我们可以指定一些只在编译时(g++ main.cpp)而不是在运行时(./a.out)执行的指令吗? 在 C/C++ 中 - can we specify some instructions which only execute at compile time(g++ main.cpp) not at runtime ( ./a.out) ? in C/C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM