wvn
wvn

Reputation: 675

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. 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:

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)

I.e., I am compiling each of these "hybrid" files twice, once as a cpu-only object and once with gpu code as well.

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. Unfortunately, if I write a simple "Hello World" program with extension ".cppx" then g++ will not compile it.

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.

Is there an easy way to forcefully instruct g++ to compile the source file with an arbitrary extension?

Upvotes: 2

Views: 385

Answers (2)

Oliv
Oliv

Reputation: 18081

You can explicitly specify the language of a source file using the -x <lang> option:

g++ -x c++ file.strange_extension

Upvotes: 5

just a guy
just a guy

Reputation: 137

you could use a string variable in your makefile which includes the compiler options and use it for every cpp file.

Upvotes: 0

Related Questions