1

I have a question on how to compile a C++ program in Terminal Mac. My program has a header file and a main file. I know that I can't compile both the header file and the main file. and just to compile the main file. I also know that I need to create a name for storing the compiled file. Here is my compile command that I used g++ -o execute1 main.cpp and I get this:

Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
  _main in main-f2nZvj.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

How can I fix this? Any help will be greatly appreciated. If it helps, below is my code for the two files:

add.h:

int add(int x, int y);

main.cpp:

#include <iostream>
#include "add.h"

int main(){
    using namespace std;
    cout << "The sum of 9 and 9 is " << add(9, 9) << endl;
    return 0;
}
user2967353
  • 257
  • 3
  • 9
  • 18
  • Do you have an `add.cpp` somewhere ? – Paul R Dec 02 '13 at 18:42
  • @bamboon: not true - gcc/g++ have symbolic links to their more modern counterparts these days. – Paul R Dec 02 '13 at 18:47
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) –  Dec 02 '13 at 18:48
  • `If it helps, below is my code for the two files` .... and where is `add.cpp`? – nhgrif Dec 02 '13 at 18:48
  • @PaulR Sorry for deleting my comment. I first entered "g++" in a ssh session and thought you were right. Do you have any source for your claim? My g++ is still 4.2. To restate what I said, I claimed that mac osx ships with gcc 4.2 and that one shall use clang. – Stephan Dollberg Dec 02 '13 at 19:01
  • @bamboon: you may need to install the CLT package for whatever version of Xcode you are using - with Xcode 5 I get the following for `g++ -v`: `Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)` – Paul R Dec 02 '13 at 19:07
  • @PaulR So it was a misunderstanding, I thought you meant that mac osx ships with e.g.: 4.8. `gcc` seems to be a symlink to clang which is configured to use gcc 4.2 include dir. – Stephan Dollberg Dec 02 '13 at 19:16

2 Answers2

5

You need an add.cpp file that implements your add() function, then you can compile the whole thing as:

$ g++ -Wall main.cpp add.cpp -o execute1
Paul R
  • 208,748
  • 37
  • 389
  • 560
0

This line:

int add(int x, int y);

in your add.h just tells the compiler that somewhere, there's a function called add that takes two integers and returns an integer. Having this and this alone will let the compiler leave you alone when you use this add function in files that #include "add.h". The compiler doesn't have to know exactly what the function does, it just needs to know what parameters it accepts and what the function returns. It doesn't bother looking for the function body until it actually goes to compile the function.

In order for this to properly compile, you need to include a function body for your add function in add.cpp. Even just this will work:

int add(int x, int y) {
    return 1;
}

This will allow the program to compile because now the compiler know what code it's supposed to execute when it gets to your call to the add function within main.

This will work, as a minimum, as a placeholder until you're ready to actually write the exact logic you want this function to contain. But until this function body exists, you won't be able to compile (unless you remove all the other references to the function).

nhgrif
  • 61,578
  • 25
  • 134
  • 173