What does the compile-time error "Undefined symbols for architecture x86_64" mean? - compilation

I'm trying to program a graph class using an adjacent list from an example in my C++ text book, and when I compile using this command:
Code:
g++ -o prog program.cpp
...I get the following error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
... what in the world does this mean? It may turn out to be an issue with my code, but I feel like it may be deeper than that, because I've gotten this same seemingly inexplicable error for several different projects, many of which were solved in different ways, and unfortunately completely by accident.
I read somewhere that it may have to do with whether I'm using 32 bit or 64 bit libraries, and that the tags -m32 or -m64 may need to be used, but I'm not sure if this applies here. Interestingly enough, when I tried adding the -m64 tag I got the same exact error, but when I tried using the -m32 tag I got the same error, except it said
Undefined symbols for architecture i386:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
instead.
Mainly I just want to know what in the world the error is saying. I'm used to debugging compile-time errors that give a specific line in the code, etc., but I can't discern anything like that from this. Any ideas?
If it helps, I'm using a late 2008 Macbook with Intel Core 2 Duo, (so 64-bit), and I'm running OS X Lion (10.7.2), which I think is the latest version. Also, I'm using gcc version 4.2.1.

When you compile the file, the compiler invokes the linker which tries to generate an executable. But it cannot because you didn't provide a function named main which is the function that will be executed when your program is launched.
Either you don't want to run the linker because you want to compile several files separately then combine then. In that case, use the -c flag to tell the compiler to skip the link stage.
Or either you want to execute the compiled file. Then you need to implement the function main.

Related

Darwin `ld` stubbornly won't find symbol that's defined in object file, while `lld` does

When using macOS's built in ld, I get the following error:
ld -arch x86_64 -platform_version macos 11.0 12.2.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -lSystem -lc++ -o /Users/blashyrk/Documents/source/nim/asserttrigger /Users/blashyrk/.cache/nim/asserttrigger_d/asserttrigger.o
Undefined symbols for architecture x86_64:
"_nlvmEHPersonality", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in asserttrigger.o
ld: symbol(s) not found for architecture x86_64
However, that symbol is a function that's defined inside that very same object file.
Using objdump on the object file, I can confirm that the function is indeed there:
objdump -t ~/.cache/nim/asserttrigger_d/asserttrigger.o | grep nlvmEH
00000000000063b0 l F __TEXT,__text _nlvmEHPersonality
And just to make sure I wasn't crazy, I decided to see for myself with Ghidra (please see attached image).
So my question is, what makes macOS's ld not find this symbol? When using LLVM's lld it works, so the symbol is defined properly.
Am I, perhaps, missing a flag? Is ld expecting the EH personality function to be a part of some external object/library?
Here's also a link to the object file, if someone wants to try it out on their end: https://www46.zippyshare.com/v/0UFxM7zD/file.html
The program (if you manage to link it successfully) should run and exit with no output.
I've gotten it to work by changing the linkage type of nlvmEHPersonality from internal to external.
For some reason, ld64 cant't find it with internal linkage even though lld can.

Fortran: Undefined symbols for architecture x86_64

I am using the gfortran compiler on mac osx mavericks. I have installed Xcode and command line tools. I am trying to compile a simple helloworld program with gfortran to test whether everything works, but unfortunately it doesn't work. The error message seems to suggest that I have no main function, but I don't see the error in the code:
program hello
print *, "Hello World!"
end program hello
But when i try to compile using
gfortran helloworld.f
I get the following error:
Undefined symbols for architecture x86_64:
"start", referenced from:
-u command line option
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I do not understand what is wrong and I am on the verge of despair. I tried with gfortran 4.8.2 as well as gfortran 4.9.0, I tried reinstalling Xcode and Command Line Tools but nothing seems to work.
I am aware similar questions have been asked, but I was not able to find a solution to my problem. I would be happy even with a sloppy workaround, as long as I can get the compiler to run...
Oh my dear. Found the solution about a minute after staring the bounty. Rather than using the recommended installer from https://gcc.gnu.org/wiki/GFortranBinaries I just installed it with
brew install gcc
The gfortran now compiled without complaining.

Is there a way to use GrowlApplicationBridge without NSClassFromString?

I'm trying to build a basic cocoa app that uses Growl.
Every time I want to use GrowlApplicationBridge I have to use something like
Class GAB = NSClassFromString(#"GrowlApplicationBridge");
[GAB performSelector:#selector(setGrowlDelegate:) withObject:self];
I would like to be able to just use
[GrowlApplicationBridge setGrowlDelegate:self];
Here's the compiler error that I get when I try the behavior that I want:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_GrowlApplicationBridge", referenced from:
objc-class-ref in StatusMenuAppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
As long as you're importing <Growl/Growl.h> at the top of that file and linking the framework into your app, you should be able to use [GrowlApplicationBridge setGrowlDelegate:self] just fine.
Check the top of the file and make sure you're importing it, and check your target's “Link Libraries and Frameworks” build phase and make sure you're linking it.

Cant build Firebreath when including libgpgme

Undefined symbols for architecture x86_64: "_libintl_dgettext", referenced from:
_gpg_strerror in libgpg-error.a(libgpg_error_la-strerror.o)
_gpg_strerror_r in libgpg-error.a(libgpg_error_la-strerror.o)
_gpg_strsource in libgpg-error.a(libgpg_error_la-strsource.o) ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
As stated by the question , When trying to build my firebreath project which requires libgpgme on MacOSX 10.6 using Xcode I get the error above. My include/search paths are valid since I can run the following command from the console and it works fine.
g++ -o test test.cpp `gpgme-config --libs`
The output form the gpgme-config is -L/opt/local/lib -lgpgme -lassuan -lgpg-error. Not sure what other info is needed , comment and I will updated as needed.
Your problem is that you're building a universal binary for both 32 and 64 bit and the library you're including is not built for 64 bit. You can build i386 only by adding -D CMAKE_OSX_ARCHITECTURES="i386" to your prepmac.sh command-line.
Make sure to delete your build directory before running prepmac.sh with the new argument or it may not take.
See the Prep Scripts page on firebreath.org for more info.

Xcode 4 FLTK 1.3.0 undefined architecture x86_64

Hi, I am using Xcode 4 and trying to set up FLTK 1.3.0 to run Bjarne
Stroustrup's Chapter 12 FLTK Demo at the end of the chapter. I keep
getting the following error when compiling, and have no idea where to go.
I have an idea it might have to do with the linker flags, but I don't know
what flag to add and where...
Here's the error:
Undefined symbols for architecture x86_64:
"Fl_JPEG_Image::Fl_JPEG_Image(char const*)", referenced from:
Graph_lib::Image::Image(Point, String,
Graph_lib::Suffix::Encoding) in Graph.o
"Fl_GIF_Image::Fl_GIF_Image(char const*)", referenced from:
Graph_lib::Image::Image(Point, String,
Graph_lib::Suffix::Encoding) in Graph.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
Are you passing the linker flags present with --use-images? try:
> fltk-config --use-images --ldflags
and check the results to make sure you're getting either the system image libs, or the ones optionally built with fltk.
I also found this to be quite useful:
fltk-config --compile test.cpp
It worked perfectly for me.
For more examples of using fltk-config check out http://www.fltk.org/doc-1.1/basics.html

Resources