I teach a computer architecture course. I am trying to provide working Intel examples in both Mac OS and Microsoft Visual Studio environments. I am using Xcode 7.2, build 7C68. I am receiving the following message in the link step:
Undefined symbols for architecture
I have tried varying the build architecture between x86_64 and i386, as well as the default, with no effect. The main program and assembler function are trivial - I just need a working base. Here is the main program:
#include <iostream>
int AsmSub(int);
int main(int argc, const char * argv[]) {
int someVariable ; /
someVariable = -1;
std::cout << "Calling AsmSub\n";
someVariable = AsmSub(someVariable);
std::cout << "Back From AsmSub, value:" << someVariable << "\n";
return 0;
}
Here is the assembler function being called, which just returns (I am assuming that EAX will contain both the single parameter and the result, but that is unrelated to this problem):
.global AsmSub
.text
.align 16
AsmSub:
ret
.data
.end
Here are the link step error messages:
Undefined symbols for architecture x86_64:
"AsmSub(int)", 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 certain that this is probably a trivial oversight on my part, but I have not found anything like a useful answer with several searches. Any help will be truly appreciated. I have already used Google and Stack Overflow - hoping someone can run this on their own machine and see if it fails for them as well.
Best regards,
Hank Murphy
With the understanding that this is really beyond my competence, I would ask what's the name of (specifically, the extension) of the file that contains this code? IOW, are we in C++ or Objective-C++?
I ask because function 'main' is apparently using the C ABI (its actual global name has a prepended underscore), but AsmSub is apparently not. If the call is using the C++ ABI, then its name is mangled, and your .global directive has the unmangled — i.e. wrong — name. This idea is (slightly) supported by the fact that the undefined symbol is reported with its parameter type [AsmSub(int)], which would be irrelevant to a plain C calling sequence.
So, does it work if you declare the AsmSub prototype to be extern "C"?