Search code examples
iosxcode4linkerobjective-c++

Link error with Objective-C++/C++ in XCode 4.2 Lion (iOS product)


I am new to Objective-C and a bit rusty at using C++ and templates, and I am not sure why I am having this link error.

I have a header file that contains definitions similar to these:

struct Info {
  std::string name;
};

typedef std::map<std::string, Info> InfoMap;

void Validate(InfoMap* infoMap);

@interface InfoValidator : NSObject 
{ 
}
+(InfoValidator*) getInstance;
-(void) validate:(InfoMap*)infoMap;
@end

I also have an .mm file that contains the following definition for the (global) Validate() method:

void Validate(InfoMap* infoMap)
{
    [[InfoValidator getInstance] validate:infoMap];
}

When I call Validate() from a C++ class (defined in an .mm file), I receive the following error:

Undefined symbols for architecture armv7:   
Validate(std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Info, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, > std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, ?> std::allocator<char> > const, Info> > >*)", 
referenced from:
      ItemInterface::ValidateItems(int, char const**)in iteminterface.o 
ld: symbol(s) not found for architecture armv7 
collect2: ld returned 1 exit status

The file has been added to the project, and it is the only link error I get. Thanks for any help.


Solution

  • Did you rename Market_Validate to Validate for the code you've quoted here? The link error refers to the former, which doesn't exist in the code given.

    If so, the code looks correct in isolation, I suspect there's something outside the code you've shown going wrong. Make sure both .mm files are in fact being compiled and linked. Check that both include the same header file with your Validate() function declaration. Since you have renamed the function before posting it here, make sure you're using the same name for it throughout your code (specifically, the declaration and definition must match exactly).

    Are you using namespaces anywhere? Make sure you didn't accidentally put your function definition inside a namespace if the declaration isn't - they'll be referring to different functions.

    Note that the code as you've posted it won't compile, as there's a semicolon missing after your struct Info definition. It's often impossible to help once the code has been altered for the question without testing that the problem still exists with that code. If you're still stuck, I suggest trying to amend the code in this question so it will compile and show the same link error. If you can't reproduce it, take a good look at the differences between your 'real' code and the question code.