I have a very small program in Xcode only displaying a label and changing the text of the label in the viewWillAppear method. The label.text should come from a C++ library with a function like this:
int getNumber(){
return 42;
}
The problem is, that including the class with #import "TestLibMain.h" in my *.mm(!) class and using the function with
TestLibMain *tlb = new TestLibMain();
int myInt = tlb->getNumber();
NSString *myString = [NSString stringWithFormat:#"%d",myInt];
doesn't invoke a compiler error, but a linker error:
Undefined symbols for architecture i386:
"TestLibMain::getNumber()", referenced from:
-[tbViewController buttonPressed:] in tbViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My impression is, that the cpp-class has not been compiled.
I tried a lot of things around, but somewhere it's hanging. It's all in the same directory, I use the .mm extension, everything fine, but always this linker error. Getting crazy :-)
Mac OS X Lion, XCode 4.2
Any ideas?
int getNumber() {
return 42;
}
in a .cpp or .mm defines getNumber() as a free function. This:
int TestLibMain::getNumber() {
return 42;
}
defines getNumber() as a member of TestLibMain.
Related
I am learning to code in C++ and am working in Xcode9.1 on OS X 10.13.1. While trying to understand the use of keyword extern, I encountered the problem that the following code:
extern int foo;
#include <iostream>
int main() {
foo = 7;
std::cout << foo << std::endl;
return 0;
}
results in a linker error when run:
Undefined symbols for architecture x86_64:
"_foo", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am not sure why the linker cannot find foo despite the definition being the first line in main.
Thanks very much for looking into my problem!
The linker cannot find foo, because it's not defined anywhere. By declaring extern int foo', you're telling the linker that the definition is somewhere else. Remove extern, or define foo somewhere where the linker can find it.
Have a look at this example on Wikipedia.
I have two projects, both built with clang++ and Xcode on MacOS.
I have a library with a header defined as follows.... (serialization.h)
#pragma once
#include <visionApp/cv/matchers/visual_database.h>
namespace visionApp {
void serializeDB(visionApp::VisualDb visualDB, std::string fileName);
visionApp::VisualDb* deserializeDB(std::string fileName);
}
The cpp file is as follows.... (serialization.cpp)
#include "serialization.h"
namespace visionApp {
void serializeDB(visionApp::VisualDb visualDB, std::string fileName)
{
}
visionApp::VisualDb* deserializeDB(std::string fileName)
{
return new visualDB();
}
}
The method is then called in another class.....
void saveRecogniser(std::string fileName)
{
serializeDB(currentVisualDB.get(), fileName);
}
void loadRecogniser(std::string fileName)
{
mVisualDatabase.reset(deserializeDB(fileName));
}
Note: currentVisualDB is a shared pointer to .get() returns a pointer.
This all builds fine. Which is great...... and make libvisionApp.a
But when i build a dependent application that can only see the headers i get the following error....
Undefined symbols for architecture x86_64:
"visionApp::serializeRecognizer(visionApp::VisualDb*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
visionApp::Recogniser::saveRecogniser(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in libvisionApp.a(planar_recogniser.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Now, this linking error suggests that clang is not able to link the serialize method, but it has no problems with the deserialize method. Removing the serialize method, and leaving only the deserialize builds correctly.
I do not understand this issue, and am unsure how to proceed. Can someone educate me as what to do in a situation like this?
Any advice on how to tackle this issue?
Note: Lipo output for the library in question.
Hal:Release daniel$ lipo -info libvisionDB.a
input file libvisionDB.a is not a fat file
Non-fat file: libvisionDB.a is architecture: x86_64
You have declared serializeDB to get first parameter by value.
void serializeDB(visionApp::VisualDb visualDB, std::string fileName);
When you call the function, you use a pointer as first parameter:
void saveRecogniser(std::string fileName)
{
serializeDB(currentVisualDB.get(), fileName); // You said that currentVisualDB is a std::shared_ptr
}
I don't know why your build gets to linking stage, it shouldn't. Maybe you have more than one place where you declare serializeDB?
I am creating a dynamic library.
foo.h
extern unsigned int myoperator;
int operate(int a, int b);
foo.cpp
#include "foo.h"
int operate(int a, int b){
switch(myoperator){
case 0:
return a+b;
case 1:
return a-b;
default:
return a*b;
}
}
libfoo is built exceptionally well on linux gcc C++14, however its throwing a linker error in macOS clang C++14. The error is
Undefined symbols for architecture x86_64:
"_myoperator", referenced from:
operate(int, int) in foo.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The closest link to the issue I got on google is https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
I am not sure if the answer is in it.
If the "real" definition of 'myoperator' variable is in another file which is part of the same project foo belongs to, I think it should be better if you put the line code
extern unsigned int myoperator;
in your cpp file
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
I just came across something that I failed to understand.
I had a problem at the linking stage in the following case.
//header file
class A
{
template<class T>
std::weak_ptr<T> GetSomethingFromSomeWhere(const char* Id);
};
//cpp file
template<class T>
std::weak_ptr<T> A:GetSomethingFromSomeWhere(const char* id)
{
//A method with the right stuff inside and the right return statement
...
}
//Another class
class B
{
};
//main.cpp
int main ()
{
A a;
auto pB = a.GetSomethingFromSomeWhere<B>( "id" );
}
This didn't compile, during linking I have something of this kind :
Undefined symbols for architecture x86_64:
"std::__1::weak_ptr A::GetComponentFromName(char const*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I fixed it by defining the template method directly in the header file.
Should I always define template method in the header ? Why ?
I am on OSX and use clang++ with XCode if that can be of any help
Thanks
Template definition needs to be visible to the code using it. Otherwise linker errors will be generated.
There are different workarounds for situations like that:
Read This
interface:
class rmKeyControl {
static map<char, function<char(char)>> sm_function_list;
public:
static bool addKeyAction(char, function<char(char)>);
};
implementation:
bool rmKeyControl::addKeyAction(char key, function<char(char)> func) {
if (!sm_function_list.count(key)) {
sm_function_list.insert(pair<char, function<char(char)>>(key, func));
return true;
} return false;
}
The full error message is:
Undefined symbols for architecture x86_64:
"control::rmKeyControl::sm_function_list", referenced from:
control::rmKeyControl::addKeyAction(char, std::__1::function) in rm_KeyControl.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This seems to be a standard linker error for Xcode 4, but it seems to occur for all sorts of reasons, and it never elaborates. This error seems to indicate the presence of binary instructions that don't work on the x86_64 architecture, but that doesn't make sense in this context. Why am I getting this error?
Edit: I forgot to mention that rmKeyControl is in namespace control. I am using namespace control; in the implementation, although you cannot see it.
Static member is just declaration. Define it in the implementation/source file like-
// include interface header and then do -
map<char, function<char(char)>> rmKeyControl::sm_function_list;