Question

I have a C++ program, about 7 header files and 7 .cpp files all jammed into one directly. If I attempt to compile it in CodeBlocks, everything works fine and dandy. No problems at all.

However, I'm trying to get it to work under a Unix command line. I'm doing a basic compile by typing

g++ main.cpp

This should produce a file called a.out (by default), which I am to run.

However, I get a load of undefined reference errors. Why is this happening and how can I fix it?

Was it helpful?

Solution

I find this question strange because CodeBlocks uses G++.

When you compile with the command line g++, you need to specify all the source files that need compiling and linking:

g++ -o main main.cpp file1.cpp file2.cpp  

You will also need to include the libraries with the -l option. The libraries depend on your OS and platform.

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