Cross Compiling for x64 on ARM (Apple Silicon) - c

It is definitely possible to target Intel when compiling on an Apple Silicon (ARM64) system, as Xcode does that all the time when building universal bundles of an app. However, I am unable to replicate this compiling a C program with make (specifically Stockfish).
What I've tried
I'm invoking make like so: make build ARCH=x86-64-modern COMP=clang (the same command works when I substitute x86-64-modern for apple-silicon). I've tried using the gcc compiler, which also worked when targeting the apple-silicon arch.
The problem
The make build command terminates with a bunch of errors, most importantly:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Above the error is a bunch of lines complaining that a file was built for an "unknown bitcode architecture:
ld: warning: ignoring file <name>.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): <name>.o
So, it seems like the compilation phase succeeds, but the linking phase fails due to missing symbols. How would I acquire and provide the missing symbols to ld such that it can link successfully?

Related

Undefined symbols for architecture arm64, Building C files in xcode

What I am trying to do is take a bunch of .C files that I created using f2c.exe on a Windows computer and use them in Xcode in an already existing project consisting primarily of Swift code.
On my Windows computer I used f2c to generate the .C files and then was able to build them to an executable and it runs as expected. So I took all of the .C files and brought them to my Xcode project and that is where I am currently stuck.
I only just added in the .C files and Xcode gives the following errors when I try to buid:
Undefined symbols for architecture arm64:
"_s_wsle", referenced from:
_xplannr_ in X1.o
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
X1.C is one of the .C files I have as an example. Additionally there are many repeated messages, I have just shown 1 for the purpose of this question. They all follow the same format as what I have provided here.
So what I am trying to understand is, what does the error message that is being provided indicating? Is this an issue of how the .C files were generated, or some configuration issue within Xcode that is not correct? Any ideas are appreciated.

I always get an error on VS code when I code in c

I have just started coding in c on my MacBook Air(m1). But whenever I code anything on VS code it throws the following error.
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have installed all the extensions and the clang as well but it always shows this error. I am new to coding and have very little idea about it.

Compiling a C program that uses OpenGl 4 in Mac OS X

So I am learning OpenGL 4 from openglbook.
I copied and pasted the simple window creating program in my editor and followed Compiling OpenGL programs on OS X by using
gcc -o hello hello.c -framework OpenGL -framework GLUT
I got the following error:
Undefined symbols for architecture x86_64:
"_glewGetErrorString", referenced from:
_Initialize in chapter-c8ba2d.o
"_glewInit", referenced from:
_Initialize in chapter-c8ba2d.o
"_glutInitContextFlags", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutInitContextProfile", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutInitContextVersion", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutSetOption", referenced from:
_InitWindow in chapter-c8ba2d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I understand correctly, the problem is tha -framework GLUT means that you're linking against the GLUT framework shipped by Apple with macOS and located inside /System/Library/Frameworks folder. This GLUT implementation unfortunately lacks some functionality provided by current version of FreeGLUT. And OpenGLBook.com tutorials use some of this functionality, thus, as it is mentioned many times here, the code examples have to be compiled with FreeGLUT.
There are, of course, few ways this can be done. The easiest one is by installing freeglut by means of the infamous Homebrew package manager and then setting up the CMake project as described below.
So, first install freeglut
brew install freeglut
Then setup CMake project by adding the following lines into CMakeLists.txt
find_package(FreeGLUT CONFIG REQUIRED)
find_package(glew CONFIG REQUIRED)
link_libraries(FreeGLUT::freeglut GLEW::GLEW)

El Capitan: Undefined symbol "start" whenever I compile a C program

I recently installed OS X El Capitan, and after I installed it I've been having a problem with my C linker. Whenever I try to compile any program, it says
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
This happens whether I use Clang installed from the Xcode developer tools, Clang installed from Homebrew, or GCC installed from Homebrew. It only started happening right after I installed El Capitan.
I've encountered similar issue.
After I upgraded my macpro to OS X El Capitan, I became unable to compile my program with GNU gcc installed from Macport. It shows same error message. However clang worked fine in my case.
I searched web and found this:
Undefined symbol "start" while linking D program through LD
It says add -lcrt1.o in gcc option.
I tried it and it did fix the problem, so you may try this one.
but I found more complete fix (below), so please try this one too.
In my case, problem was that I had two versions of ld: /usr/bin/ld and /opt/local/bin/ld. Somehow clang works fine with both versions of ld, but GNU gcc works well only with /usr/bin/ld.
So I changed PATH variable to tell GNU gcc to use /usr/bin/ld, then problem was fixed.

OpenSSL ecc function with macos xcode?

I am trying to use the function EC_KEY_new_by_curve_name(NID_secp256k1) present in openssl. However, when I compile, I get the following error:
undefined symbols for architecture x86_64:
"_EC_KEY_new_by_curve_name", referenced from:
CKey::CKey() in bitcoin.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 have been hinted that macos openssl is not compile with the ECC extensions. Is the problem? If so, how do I correct it?
If you're using the OpenSSL library that comes with MacOS SDK, then it should automatically have 64-bit support built in.
Looking at the first hit on Google, I'm wondering if you are just not including the correct library in your XCode project. Did you get -lcrypto into the project settings, or forget to add libCrypto.dylib to your list of libraries in the project?
On my SnowLeopard (10.6) machine, I see the symbols are defined in libCrypto:
[/usr/lib]:; nm -arch x86_64 libcrypto.0.9.8.dylib | egrep -i new_by_curve
00000000000a4ac0 T _EC_GROUP_new_by_curve_name
00000000000ab540 T _EC_KEY_new_by_curve_name

Resources