1

I have a project with multiple files.. I want to compile it using gcc from command line.

the directory looks like this

lib/
    Comp/                              contains .cpp files
    Decomp/                            contains .cpp files
    Globals.cpp
include/
    Comp/                              contains .h files
    Decomp/                            contains .h files
    Globals.h

some of these .h files are not paired with .cpp files

to compile this i use something like this :

g++ lib/Comp/* lib/Decomp/* lib/Globals.cpp -std=c++0x -o TEST

the problem is,I have to add some #defines for each .h file and i have to do it through command line. how to do this ??

also if i had to compile each file on its own and then link them. what would be the appropriate order for doing this ?

4

2 回答 2

1

The dirtiest ugliest way is that you want to use something like:

g++ -Iinclude lib/Comp/*.cpp lib/Decomp/*.cpp lib/Globals.cpp -o test

Your .cpp files should #include <Comp/foo.h> or whatever

The correct way to manage this is to use a makefile to build each object file and then link them together:

Makefile

Create a a file called Makefile and put the following in it:

CXX=g++
CPPFLAGS=-Iinclude -DFOO -DBAR=1 -DSOME_STRING=\"My Name\"
CXXFLAGS=-O2 -g 

SOURCES=lib/Comp/file1.cpp \
        lib/Comp/file2.cpp \
        lib/Comp/file3.cpp \
        lib/Decomp/file1.cpp \
        lib/Decomp/file2.cpp \
        ... 

OBJ=$(SOURCES:%.cpp=%.o)


default: test

test: $(OBJ)
<tab>     $(CXX) -o $@ $(OBJ) 

%.o: %.cpp
<tab>     $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $< 

NOTES

Replace file1.cpp etc. with the actual filenames in your project. DO NOT include headers in SOURCES only your .cpp or .cc files

If you are using sub-paths like #include <Comp/foo.h> or #include "Comp/foo.h" in your source files then you only need to use -Iinclude in CPPFLAGS but if you are doing something like "foo.h" and foo.h is actually in include/Comp/ then add -Iinclude/Comp and -Iinclude/Decomp to the CPPFLAGS

Where it says <tab> make sure you use the TAB key to insert a tab (don't type the word '')

Before using this Makefile blindly . Know that it will NOT work as is you have to correct the entries. It is offered as a starting point... Read up on writing Makefiles ... http://frank.mtsu.edu/~csdept/FacilitiesAndResources/make.htm has a good introduction

于 2013-05-09T07:35:45.007 回答
1

Defines can be provided on the compiler command line using -DVAR=VALUE (on Windows, presumably /DVAR=VALUE). Note that you can not provide different defines for different headers as in:

compiler -DX=one first.h -DX=two second.h third.cc -o third.o

In such a case, my compiler spews warning and uses the last value of X for all source code.

Anyway, in general you should not list header files on the compilation line; prefer to include them from the implementation files (.cc/.cpp or whatever) that need them.

Be careful too - if you're changing defines to modify class definitions, inline function implementation etc. you can end up with technically and/or practically undefined behaviour.

In terms of how best to decide which objects to create and link - you probably want one object per .cc/.cpp file. You can link those objects then specify them on the command line when compiling the file containing main().

于 2013-05-09T07:50:38.120 回答