Macosx, C and embedded lua - c

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

Related

Undefined symbols for architecture arm64: `"_puts"`

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

Mac mini m1 + VS Code + C Programming = Not Working

I am self learning to code in C using Mac mini M1 and VS Code. I get the below error:
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Please note:
Installed the VS Code Insiders for ARM apple version!
Clang is installed!
Xcode command line tools installed!
C/C++ 1.4.0-insiders2 extension is installed in VS Code!
Code Runner 0.11.4 is installed in VS Code!
Code:
#include <stdio.h>
int main()
{
printf("test");
return 0;
}
Compiled using:
gcc -Wall -Wextra -Werror -o test test.c
Okay! It's because you forget to save your program before executing i.e you should compile and run. Instead of just running it! #Keeplearning!

Compile Lua from source and create C module with it

I thought of compiling Lua from source code and then create a C module.
I compiled Lua with success but I can't build my C module.
So, I compiled Lua like this:
gcc -o Lua *.c -Os -std=c99
Compiled my module like this:
gcc -Wall -shared -fPIC -o module.so -I. module.c
But there are a few errors here:
Undefined symbols for architecture x86_64:
"_lua_pushcclosure", referenced from:
_luaopen_module in module-fb0b1f.o
"_lua_pushnumber", referenced from:
_super in module-fb0b1f.o
"_lua_setglobal", referenced from:
_luaopen_module in module-fb0b1f.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 C module itself:
#include "lua.h"
static int super(lua_State* L) {
lua_pushnumber(L, 5);
return 1;
}
int luaopen_module(lua_State* L) {
lua_register(L, "super", super);
return 0;
}
My Lua script:
require("module")
print(super())
I'm on Unix based system (Mac), but I want it to work on Linux as well.
Edit:
Problem to compile C module was fixed by entering -bundle -undefined dynamic_lookup instead of -shared (Thanks lhf).
But I can't import the module in Lua.
> require("module")
error loading module 'module' from file './module.so':
dynamic libraries not enabled; check your Lua installation
Another thing:
This seems to be a quick fix only; -bundle -undefined dynamic_lookup. This does not work on Linux. How can I do this on linux? I wanted a solution for Unix based systems.
Download Lua from lua.org and build Lua with make macosx. See Getting started.
Use -bundle -undefined dynamic_lookup instead of -shared to build module.so.
Use require"module" to load it into Lua.
Call super.
Make sure you're running the lua program that you have built above, not some other version that is installed.

Undefined symbols for architecture x86_64: (Mac OS X 10.7)

I am working on an MP for my CS class. Our computer labs are working under Linux OS, but I tried compiling the code on my home computer (Mac OS X). I am getting the following error:
Undefined symbols for architecture x86_64:
"_tdestroy", referenced from:
_dictionary_destroy in libdictionary.o
_dictionary_destroy_free in libdictionary.o
ld: symbol(s) not found for architecture x86_64
I tried finding a solution online, but I was unsuccessful. We are using the following macros in the Makefile:
CC = gcc
INC = -I.
FLAGS = -g -W -Wall
LIBS = -lpthread
Any ideas?
From the GNU man page of tdestroy:
SVr4, POSIX.1-2001. The function tdestroy() is a GNU extension
This means that this function is not available on OS X
EDIT:
Put this after the includes:
#ifndef _GNU_SOURCE
void tdestroy(void *root, void (*free_node)(void *nodep)) { }
#endif
You can try to implement tdestroy by using twalk/tdelete/free - it should'n be very hard to do, but leaving it empty should work too (but it will create a memory leak on OSX).
EDIT 2: added link to the man page (10x to Cameron)

Mac proj.4 compile error

I installed proj.4 library with homebrew on my Mac 10.7 (using gcc-4.2). When trying to compile the following code:
#include <proj_api.h>
int main(void) {
projPJ pj_merc;
pj_merc = pj_init_plus("+proj=merc");
pj_free(pj_merc);
return 0;
}
I'm getting this error:
$ gcc-4.2 test.c
Undefined symbols for architecture x86_64:
"_pj_init_plus", referenced from:
_main in cccf4vey.o
"_pj_free", referenced from:
_main in cccf4vey.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
EDIT:
The library file is 64bit (gcc-4.2 -m32 test.c lead to the same error):
$ file /usr/local/lib/libproj.dylib
/usr/local/lib/libproj.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Any ideas what's wrong?
Thank you!
You should link against the library.
gcc-4.2 test.c -L/usr/local/lib -lproj
This is what the error is complaining about

Resources