Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zachharmonic

macrumors newbie
Original poster
Dec 23, 2009
2
0
Hello Mac Forum!

I'm kind of new with programming. I have vague experience with Java, and recently I've been learning some C++. So far, I've been having a pretty easy time with it. As the title says, I'm using Xcode 3.1.2, and I'm having a bit of an issue creating and using header files. Now, when I tried to insert a header I just went to File -> New -> Header File. As far as my code goes the header code is:
HTML:
#ifndef ADD_H
#define ADD_H
 
int add(int x, int y); 
 
#endif

And my main code is:

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

int main()
{
	    using namespace std;
	cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
	    return 0;
}

And finally, the error I get when I try to build and run is:

B
HTML:
uilding target “Header File” of project “Header File” with configuration “Debug” — (1 error)
	    cd "/Users/zh/Documents/Header File"
    setenv MACOSX_DEPLOYMENT_TARGET 10.5
    /Developer/usr/bin/g++-4.0 -arch i386 -isysroot /Developer/SDKs/MacOSX10.5.sdk "-L/Users/zh/Documents/Header File/build/Debug" "-F/Users/zh/Documents/Header File/build/Debug" -filelist "/Users/zh/Documents/Header File/build/Header File.build/Debug/Header File.build/Objects-normal/i386/Header File.LinkFileList" -mmacosx-version-min=10.5 -o "/Users/zh/Documents/Header File/build/Debug/Header File"
Undefined symbols:
  "add(int, int)", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
		  "add(int, int)", referenced from:
		      _main in main.o
		ld: symbol(s) not found
		collect2: ld returned 1 exit status
Build failed (1 error)

So, did I not insert a header right? Or does my code have a problem? Any help would be greatly appreciated! I'm trying to get ready ahead of time for a c++ class I have next semester.
 
A header file with a function declaration needs an implementation for that function. Add a new file add.c, and fill in its contents:
Code:
#include "add.h"
 
int add(int x, int y)
{
    // fill
}
 
Works! Thanks! Now, like I said I'm kind of new to this. Can you explain to me why this extra file needs to be there for the header to work please?
 
Works! Thanks! Now, like I said I'm kind of new to this. Can you explain to me why this extra file needs to be there for the header to work please?

The header file included a "function prototype." This basically claims that the function exists and describes how the code should interact with it. Note that nowhere in the header file is anything actually added together.

In order to use a function, the compiler has to know its interface (that it receives two integers and provides an integer as the result). To know the interface, the compiler must either see a prototype or see the actual function implementation itself. Often, the actual implementation will be in a different .cpp file which will be compiled into its own unique object file. Headers allow a .cpp file to access functions that have been defined in a different .cpp file.

You claimed that a function existed and tried to actually use the function without ever making that function exist. This shows up as a linker error because this is when the function call is resolved. The extra file did not need to be there. What was needed is the implementation of the function. That could have been in your existing cpp file, but that would have bypassed the purpose of the .h file.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.