[英]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.