0

I wish to compile many .cc files into one .o file.

I'm using a bash script, which I need to do, to build my unit test source files into a unit_test.o file, but it's saying "g++: cannot specify -o with -c or -S with multiple files" I link these source files in the next stage of the script.

So far I have;

g++ -Wno-deprecated -std=c++0x \
    -o $Unit_Test_Output_Directory/unit_test.o \
    -c $Unit_Test_Source_Directory/test1.cc $Unit_Test_Source_Directory/test2.cc

Then after that I have my -I's and -L's, the "\" in bash is used to split commands onto multiple lines.

Then in the linking stage I link my unit_test.o and my source file's .o's into a unit_test_executable.

Can somebody help me out there? Do I need to keep calling the build function for -o unit_test1.o -c test1.cc, and then for test 2... and link unit_test1.o and unit_test2.o in the linking stage?

I've also unsuccessfully tried:

g++ -Wno-deprecated -std=c++0x \
    -o $Unit_Test_Output_Directory/unit_test1.o  \
    $Unit_Test_Output_Directory/unit_test2.o \
    -c $Unit_Test_Source_Directory/test1.cc \
    $Unit_Test_Source_Directory/test2.cc
lukegjpotter
  • 241
  • 3
  • 5
  • 13
  • You basically can't do this: the .o files are 1-1 object files for source files. – Joe Mar 13 '13 at 13:29
  • 1
    @Joe sure, but I suspect lukegjpotter is asking the wrong question. – Yakk - Adam Nevraumont Mar 13 '13 at 13:39
  • You can always concat source files int one temporary and compile it. It would have too many side effect however, do not do that. Why do you even want it? Make archive using ar instead, if you want one file. You could also use --combine, but that does work only for C compiler. – Pihhan Mar 13 '13 at 16:18

1 Answers1

1

You can't compile multiple source files to a single object file. Each source file has to be compiled to its own object file.

You have to do it separately for each source file:

g++ -Wno-deprecated -std=c++0x \
    -o $Unit_Test_Output_Directory/unit_test1.o  \
    -c $Unit_Test_Source_Directory/test1.cc

g++ -Wno-deprecated -std=c++0x \
    -o $Unit_Test_Output_Directory/unit_test2.o \
    -c $Unit_Test_Source_Directory/test2.cc
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621