Undefined symbols for architecture arm64: `"_puts"` - c

I am writing a simple "Hello world" program using MacBook Air 2020 (M1) and the C code is as follows:
#include<stdio.h>
int main(void){
printf("Hello world!\n");
return 0;
}
clang version is
Apple clang version 14.0.0 (clang-1400.0.29.201)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
However after I compiled it and try to run ld hello.o -o hello it gives the following error:
Undefined symbols for architecture arm64:
"_puts", referenced from:
_main in hello.o
ld: symbol(s) not found for architecture arm64
Why is the symbol not defined?

Link using the compiler (clang hello.o -o hello) — it will add the standard C library and the startup code, etc to the ld command line.
Add -v to the options to see what the compiler actually executes.
On an Intel Mac running macOS Big Sur 11.7, part of the information produced was this humungous long line which invokes the loader (ld) in a ridiculously obscure location:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -platform_version macos 11.0.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o tm59 -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib -L/usr/local/lib /var/folders/sj/_v4_1hp947d_6qg_m75syr000000gn/T/tm59-7bf6b1.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/lib/darwin/libclang_rt.osx.a

Related

Is it posible to compile Flex/Lex on a M1 Mac

In my compilers class we are writting Flex/Lex code. When I compiled the .l file and tried to compile the resultant lex.yy.c file with gcc, I got the following error:
Undefined symbols for architecture arm64:
"_yywrap", referenced from:
_yylex in lex-fb85c9.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does is posible to compile it in a Apple Silicon (M1) Mac, at least in a Linux VM?
I created a test file and compiled:
% flex lex.l
% cc -o lex lex.yy.c -lc -ll
% grep 'yylex();' lex.yy.c
yylex();
The -ll on the cc command links against the libl library. Current M1 based macOS does not provide a libfl library you may see referenced.

Trouble debugging C program in Eclipse on Yosemite

I am getting the following persistent errors trying to debug a C program in Eclipse Juno on my mac:
Building target: TimeStamps
Invoking: MacOS X C Linker
gcc -v -o "TimeStamps" ./graphic/arrow.o ./graphic/axesdraw.o . . .
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o
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)
make: *** [TimeStamps] Error 1
So, I am confused about how to fix this in Eclipse. The main routing is in timestamp_test.c
I have -g -v as compiler flags, and -v as a linker flag.
No .o files are made.
Based on the the error given, you need to add the main function to timestamp_test.c.
Or pass the file that have the main function implemented to the compiler.

ld: symbol(s) not found for architecture x86_64 (libusb)

I'm trying to compile the following libusb snippet on my Mac:
#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>
int main(void) {
libusb_device **devices;
ssize_t device_count = 0;
device_count = libusb_get_device_list(NULL, &devices);
printf("%d devices found\n", (int)device_count);
return EXIT_SUCCESS;
}
I have libusb installed via Homebrew.
I'm getting the following error during compilation:
ld: symbol(s) not found for architecture x86_64
The full compiler output is as follows:
22:28:24 **** Incremental Build of configuration Debug for project libusb ****
make all
Building file: ../src/libusb.c
Invoking: Cross GCC Compiler
gcc -I/usr/local/Cellar/libusb/1.0.9/include/libusb-1.0/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/libusb.d" -MT"src/libusb.d" -o "src/libusb.o" "../src/libusb.c"
Finished building: ../src/libusb.c
Building target: libusb
Invoking: Cross GCC Linker
gcc -o "libusb" ./src/libusb.o
Undefined symbols for architecture x86_64:
"_libusb_get_device_list", referenced from:
_main in libusb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libusb] Error 1
22:28:24 Build Finished (took 119ms)
I understand the problem is to do with the linker not finding the libusb library, right? How do I tell the compiler where that is in Eclipse CDT?
As we worked out in all the comments the link command that worked is
gcc -L/usr/local/Cellar/libusb/1.0.9/lib -o "libusb_example" ./src/libusb_example.o -lusb1.0

Macosx, C and embedded lua

Ok, what we have:
Program written on C which compiling and running without problems in Linux and MacOSX (leopard).
I embedded lua code today. Linux: compiled lua 5.1 from source. Everything works and compiles without any problems. Macos: compiled the same lua 5.1 package.
Linked with --llua.
Started compile and got an error:
CC=gcc-4.0 make
ld: warning: in /usr/local/lib/liblua.a, file is not of required architecture
Also tried reinstall, complete remove and installing from macports. The same.
So is there any fix for that?
The error "file is not of required architecture" hints to the fact that you're trying to mix architectures.
Check the architecture of the /usr/local/lib/liblua.a and make sure it matches the architecture or the object you're trying to build.
E.g.
We have a i386 object:
==== cat fun.c ====
#include <stdio.h>
void fun()
{
printf("%s", "foobar\n");
}
gcc -arch i386 -c fun.c -o fun.o
if we try to use it when compiling a x86_64 object (default architecture in Mac OS X):
===== cat test.c ==
extern void fun();
int main()
{
fun();
}
we get:
$ gcc test.c fun.o
ld: warning: ignoring file fun.o, file was built for i386 which is not the architecture being linked (x86_64)
Undefined symbols for architecture x86_64:
"_fun", referenced from:
_main in ccXVCQhG.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Erlang NIF Test -- OS X Lion

I'm trying to compile the NIF Test from Erlang (http://www.erlang.org/doc/man/erl_nif.html) on Mac OS X Lion. I can't get it to compile. Am I missing a compiler flag? Here's the error I get:
Computer:~ me $ gcc -fPIC -shared -o niftest.so niftest.c -I /usr/local/Cellar/erlang/R14B02/lib/erlang/usr/include/
Undefined symbols for architecture x86_64:
"_enif_make_string", referenced from:
_hello in ccXfh0oG.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I've also tried this with -m32 but it says there's no i386 architecture either.
Thanks!
for 64-bit Erlang, the following works for me:
gcc -undefined dynamic_lookup -dynamiclib niftest.c -o niftest.so \
-I /usr/local/Cellar/erlang/R14B02/lib/erlang/usr/include
It seems like your problem is not architecture but undefined symbol _enif_make_string, which means that you have to link with your enif library, whatever it is, using -l option. Also, it's been a long time since I built a shared library for OS X, but I think that the right flag to use is -dynamiclib and not -shared, and you don't have to have a space after -I.
Try using these flags when compiling your nif instead of -shared
-bundle -flat_namespace -undefined suppress

Resources