Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a C++ program that compiles and runs perfectly in Code Blocks. However when I try to compile it from the terminal using:

gcc -g main.cpp -o Main

I get a lot of undefined references:

And this is a screenshot of the program running in Code Blocks:

share|improve this question
add comment

3 Answers 3

You should be compiling using g++, gcc compiles C programs. To install all required packages of g++, do sudo apt-get install build-essential on a terminal and then to compile your program, do g++ -g main.cpp -o Main.

share|improve this answer
    
I tried with the g++ and I'm still getting the same exact errors! –  Mark J Nov 15 '13 at 19:52
add comment

You should use g++ for C++ programs always!

That's because:

  • gcc is GNU C      Compiler

  • g++ is GNU C++ Compiler

So they are different compilers and so they are different packages. The main differences:

  1. gcc will compile: *.c / *.cpp files as C and C++ respectively.
  2. g++ will compile: *.c / *.cpp files, but they will all be treated as C++ files.
  3. if you use g++ to link the object files, it automatically links in the std C++ libraries (gcc does not do this).
  4. gcc compiling C files has less predefined macros.
  5. gcc compiling *.cpp and g++ compiling *.c / *.cpp files has a few extra macros.

Source: What is the difference between g++ and gcc?

share|improve this answer
    
I tried with the g++ and I'm still getting the same exact errors! –  Mark J Nov 15 '13 at 19:51
add comment
up vote 0 down vote accepted

I figured out the problem. I didn't find the specific solution but I found a work around. Apparently the classes aren't being linked. So I put all the classes into the main file and it worked perfectly!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.