Click to See Complete Forum and Search --> : question about gcc


Haunted
12-03-2002, 09:54 PM
Hey.

I am newbie at programming c++ and all programming that I have done before was in IDE that supports projects so I have some question about compiling stuff in the terminal.

Today I tried to compile one of my programs written in IDE, but it didn't seem to work so I have a question -

when in the beginning of my program I do this

#include <iostream>
using namespace std;

#include "something.h"
#include "somethingesle.h"

do I have to compile the something.cpp and somethingelse.cpp files seperatley or does the gcc compile them by itself when I issue a following command:


# g++ myprog.cpp -o program

?

Thankyou.

Spawn913
12-04-2002, 12:10 AM
I am assuming here that something.cpp and somethingelse.cpp contain functions that are called in myprog.cpp.

The answer to your question is yes, you have to compile the other 2 files separately because there's no way for gcc to know what other files are associated with your main file. You should also link the object files to create your binary.

Learn to use Makefile's.

Here's what your Makefile should basically have:


all: program

program: myprog.o something.o somethingelse.o
g++ -o program myprog.o something.o somethingelse.o

myprog.o: myprog.cpp
g++ -c myprog.cpp

something.o: something.cpp
g++ -c something.cpp

somethingelse.o: somethingelse.cpp
g++ -c somethingelse.cpp


after doing this, one call to 'make' will compile your code.

btw, note that in the Makefile, the rules (the g++ -... lines) are indented by a TAB. 'make' is rather strict in the format of the Makefile so you should check this well.

Haunted
12-04-2002, 12:43 AM
Thankyou Spawn913!

I found a GNU make manual so I hope soon I will be able to compile my program :)

The only problem is that when I don't know much about object files and how to link them, and so far I couldn't find much information about that. If you would give me a good link to a manual or something it would be greatly appreshiated ;)

Rüpel
12-04-2002, 03:10 AM
if you want it quick an dirty you may also give g++ more than one cpp-file to compile and link together, like

g++ -o program something.cpp somethingesle.cpp program.cpp

but getting familiar with make is much better! ;)

Spawn913
12-04-2002, 04:03 AM
The only problem is that when I don't know much about object files and how to link them, and so far I couldn't find much information about that. If you would give me a good link to a manual or something it would be greatly appreshiated

well, to start with, object files are the files (the ones with the .o extension) created after you call gcc (or g++) with the -c option.

If you need to 'link' object files, you can do it in your gcc (or g++) call:

gcc -o <binary> <object1> <object2> <object3> .....

Actually, that's all there is to linking -- just placing the needed object files in your gcc call to associate them.

The make manual should be sufficient for you to learn about make and Makefiles.

If you really want to try compiling your code now, just create a file, called "Makefile", place the things I wrote earlier there (taking note of the TABS), save, then type 'make'. That should do it.

Anyway, feel free to ask some more. I'm sure there are many here who can help.

:cool:

bwkaz
12-04-2002, 02:07 PM
Also, if your source files are C++ (so that you compile them with g++), you will have to link them with g++ as well -- gcc doesn't understand the symbol names properly, and it doesn't by default include the libstdc++ library.

You might be able to make gcc work, but I (and most other people, I think) just use g++ for linking C++ files.

Otherwise, if your files are C, then you can use gcc to link them.

shadowrider
12-05-2002, 12:53 AM
Originally posted by Haunted
The only problem is that when I don't know much about object files and how to link them, and so far I couldn't find much information about that. If you would give me a good link to a manual or something it would be greatly appreshiated ;) [/B]
i don't know too much either but this might help in a way(hopefully). i think in c or c++ family, object files are the intermediate code (low level code) produced by the compiler to translated to the machine code(executable code).
think, it's like this:
source code --> compiler(optimized) --> machine code

and when they're linking, they're just making sure and linking all the libraries needed for the program to work.
someone pls correct me for the mistakes.:D