I'm using the -c option with g++ to create a bunch of object files, and it's only letting me specify one source file for each object file. I want to have multiple files go into some of them. Is there any way to do this?
I'm using the -c option with g++ to create a bunch of object files, and it's only letting me specify one source file for each object file. I want to have multiple files go into some of them. Is there any way to do this?
In a sense, you can't. The compiler only compiles one file at a time. When you give it a list of files, either with -c or to turn into a single executable or shared library, it actually calls the compiler a bunch of times with some temporary output files (usually in /tmp or similar), and then links them after the fact.
The solution, then, is to only ask it to compile one file. Already mentioned is writing a file to compile
tocompile.cpp
--------------------
#include "file1.cpp"
#include "file2.cpp"
You could even autogenerate this file in your Makefile, but it turns out there is no need to do so, because the compiler is quite happy to read from - (standard input).
This means you can simply run
printf '#include "%s"\n' ./*.cpp | $CXX $CXXFLAGS -o combined.o -x c++ -
Note that the -x tells the compiler what language the input is, normally it guesses based on the compiler called (gcc vs g++ or similar), and the filename, but since the filename is -, we have to specify.
When doing this, assuming you are including your whole project, -fwhole-program in gcc is a good way to decrease the generated output size, note that for that to work, you need to also list all libraries needed.