Compiling multiple source files.......

O

Oliver Graeser

Ok, stupid question....

I used to write programs all into one text files and compile them.
Works, fine.
Then I discovered XCode (or IDEs in general) which appear to be really
increasing my productivity, so I use XCode with the settings for a
command line C++ tool. Works fine, too. Then I split up my source code
into many files. After figuring out the #ifndef stuff, that works great,
even better.

No on my machine everything works fine so I want to run the programs on
the linux cluster. Figured out that this requires recompiling, ok. So I
copied all the source files and run g++ - error. Same thing on my
machine - error. Looks like this:

Undefined symbols:
"AdaptiveNW::progressSystem()", referenced from:
_main in ccTK2FsI.o
"AdaptiveNW::eek:utCurrDegDist()", referenced from:
_main in ccTK2FsI.o
"AdaptiveNW::AdaptiveNW(int, int, int)", referenced from:
_main in ccTK2FsI.o


Since these are .o files, i figured that this is related to the linking
of the output files, which may or may not be something that make does in
Linux. I tried to read on make and its usage, but it seems way way too
complicated for something as simple as what I do here (I'd rather just
concatenate the source files into one....).

Is there any simple way to solve this? A hint on where to get _concise_
introductions on how to use make (i.e. how to use make to do
helloworld.cpp) would be more than enough....

Thanks, Oliver
 
I

Ian Collins

Sam said:
The following one-line makefile should be sufficient to compile and link
a C++ program consisting of one module, helloworld.cpp:

helloworld: helloworld.o

When the C++ program is composed of multiple modules, hello.cpp and
world.cpp:

helloworld: hello.o world.o

Linux's make is GNU make, which is far more advanced then the
traditional Unix make, and comes built-in with a rich set of default
rules that enables you to build stuff with just a minimum of a
specification. The default rules will be sufficient to do basic, simple,
builds.
<OT>As do most, if not all versions of make. Nothing unique about GNU
make there.</OT>
 
O

Oliver Graeser

Ian said:
<OT>As do most, if not all versions of make. Nothing unique about GNU
make there.</OT>

Thanks! So I guess I have to compile all my .cpps to .os. However if I
try now to compile the source files separately, it seems that the
compiler is unable to find the header files (which are in the same
directory??):

g++ AdaptiveNW.cpp AdaptiveNW.o

g++ AdaptiveNW.cpp -o Adaptive.o
In file included from SISNetworkNode.h:11,
from AdaptiveNW.cpp:12:
GNN.h:13:24: error: calling fdopen: Bad file descriptor
In file included from SISNetworkNode.h:12,
from AdaptiveNW.cpp:12:


All files are in the same directory and semantically o.k., they compile
in xcode. Google hinted towards some precompiled headers (.gch) which I
ought to delete, but I didn't find these anywhere??
 
I

Ian Collins

Oliver said:
Thanks! So I guess I have to compile all my .cpps to .os. However if I
try now to compile the source files separately, it seems that the
compiler is unable to find the header files (which are in the same
directory??):

g++ AdaptiveNW.cpp AdaptiveNW.o

g++ AdaptiveNW.cpp -o Adaptive.o
In file included from SISNetworkNode.h:11,
from AdaptiveNW.cpp:12:
GNN.h:13:24: error: calling fdopen: Bad file descriptor
In file included from SISNetworkNode.h:12,
from AdaptiveNW.cpp:12:


All files are in the same directory and semantically o.k., they compile
in xcode. Google hinted towards some precompiled headers (.gch) which I
ought to delete, but I didn't find these anywhere??

That look like a compiler issue, if the common -I compiler flag does not
work for you, try a gcc hemp forum.
 
J

Juha Nieminen

Sam said:
The following one-line makefile should be sufficient to compile and link
a C++ program consisting of one module, helloworld.cpp:

helloworld: helloworld.o

If XCode works anything like gcc, then you can auto-generate those
lines. With gcc it happens like this:

gcc -MM *.cc

(Or whatever the extension of your source files is.)

If XCode does not support the -MM option, then it might be named in
some other way.

In fact, you can make the Makefile itself auto-generate those lines
and then include the generated file, like this:

# List source files (not headers!) and binary name:
SOURCES=$(wildcard *.cc)
BINARY=programname

# Set up compiler and compiler options:
CXX=g++
CXXFLAGS=-Wall -W -ansi -O3
LDLIBS=

# If you are compiling a C program (instead of C++), comment out this line:
LINK.o=$(LINK.cpp)


# --- No need to modify anything below this line ---
$(BINARY): $(SOURCES:.cc=.o)

..dep:
g++ -MM $(SOURCES) > .dep

-include .dep

(Note that if the dependencies change because of added or removed
#include lines in the source files, you'll have to remove the .dep file
so that it will be rebuilt.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top