Question

I have a problem to compile multiple files together using GNU g++, so please help me.

We assume there are five files:

main.cpp : the main function 
a.h      : the header file of class A 
a.cpp    : the definition of class A 
b.h      : the header file of class B 
b.cpp    : the definition of class B

In the program, main.cpp uses class A (thus including a.h) and class A uses class B (thus including b.h).

So, how do I compile this program to generate the executive file?

I tried to use the following command, but it feedbacked the error:

undefined reference to ~(objects in class A and B)". 
command: g++ -Wall -O2 -s main.cpp

And my operating system is ubuntu 12.04. I know if I make a project with stuff like dev c++ it will automaticly link files together but I don't want to use that. I've also seen other links and sources but I couldn't find something useful for myself.

Was it helpful?

Solution

Try

g++ -Wall -O2 main.cpp a.cpp b.cpp

This will create a program a.out which is the executable. The -Wall flag just tells the compiler to issue more warnings, and the -O2 flag enables optimizations. The last three arguments are your source files. You need to give all source files that are not #include:d in any way to g++.

You can specify the executable name with the option -o (name).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top