Enable to compile sdl 1.2 project on macbook - c

I try this :
g++ -Wall affichage.c -o game -D_REENTRANT -I/usr/local/Cellar/sdl/1.2.15_1/SDL
but I have this error :
Undefined symbols for architecture x86_64:
"_SDL_EnableKeyRepeat", referenced from:
chargement_objets(img*, img*) in affichage-50cd48.o
"_SDL_Flip", referenced from:
affiche_menu(img, img) in affichage-50cd48.o
_SDL_main in affichage-50cd48.o
gcc Version :
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
sdl installed in : /usr/local/Cellar/sdl/1.2.15_1
Mac OS Catalina

Undefined symbols errors happen at link time.
So just like specifying header lookup path as -I/usr/local/Cellar/sdl/1.2.15_1/SDL , which helps at compile time, specify the library search path as -L/usr/local/Cellar/sdl/1.2.15_1/SDL/lib or something like this pointing to the right path. You may also need to add something like -lSDL to specify the library in which the functions you used are. Note that -lSDL means link against libSDL.a

Related

LD_LIBRARY_PATH is ignored by GCC

according to docs, GCC looks paths in LD_LIBRARY_PATH for linking shared library BUT it seems in my case it is ignored!
echo $LD_LIBRARY_PATH -->:/home/mehrdad/usr/lib (so LD_LIBRARY_PATH is set currectly)
i have libfoo.so in "/home/mehrdad/usr/lib" BUT :
gcc main.c -lfoo returns error :
/usr/bin/ld: cannot find -lfoo
collect2: error: ld returned 1 exit status
so what is the problem??? is LD_LIBRARY_PATH deprecated???!
but i can successfully link with explicit command :
gcc main.c -L/home/mehrdad/usr/lib -lfoo
and also I can successfully execute the a.out by just:
./a.out
it seems LD_LIBRARY_PATH is respected by OS library loader BUT NOT GCC!
my environment :
OS : CentOs 7
compiler : gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
I was wrong! thanks to Alexandre C and David Schwartz!
LD_LIBRARY_PATH is only for loader(runtime).
LIBRARY_PATH is what I need according to the docs:
The value of LIBRARY_PATH is a colon-separated list of directories,
much like PATH. When configured as a native compiler, GCC tries the
directories thus specified when searching for special linker files, if
it cannot find them using GCC_EXEC_PREFIX. Linking using GCC also uses
these directories when searching for ordinary libraries for the -l
option (but directories specified with -L come first).

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.

Compiling a c application that links to a static library on AIX

I'm trying to compile my application to link to a static library (.a file)
The command I use to build is this:
gcc -DUNIX -maix32 -o Release/bin/testApp Release/obj/main.o -ltestLib
When I build I get the following errors:
ld: 0711-317 ERROR: Undefined symbol: .test
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
Where test is a method in libtestLib.a
Also if I try and build with a dynamic lib then it is successful.
gcc -DUNIX -maix32 -o Release/bin/testApp Release/obj/main.o libtestLib.so
Can you see where I am going wrong?
Can you try specifying a path to the archive file, rather than -ltestLib?
gcc -DUNIX -maix32 /path/to/testLib.a -o Release/bin/testApp Release/obj/main.o

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)

Undefined Symbol when compiled with -O2 only on Mac

I'm working on a library that must compile on linux and mac os X. Until now, I had no problem, compiling with "-g" worked well under both OS.
I tried to compile with some optimization ("-O2") and it works well under linux but I get an Undefined Symbol when I try to link a program with my library under mac os X.
Does anyone have any clue what I should look for?
nm mylib.a | grep _the_symbol
This returns same thing for linux and mac (no leading underscore under linux) :
154:00000018 C _the_symbol
377: U _the_symbol
Here is the compile line under linux for the program using the library:
/usr/bin/gcc -std=c99 CMakeFiles/prod-cons.dir/prod-cons.c.o -o prod-cons -rdynamic -L/home/claferri/dev/build/src ../src/libckaapi.a -lpthread -Wl,-rpath,/home/claferri/dev/build/src
And under mac :
/usr/bin/gcc -std=c99 -Wl,-search_paths_first -headerpad_max_install_names -fPIC CMakeFiles/prod-cons.dir/prod-cons.c.o -o prod-cons -L/Volumes/Data/claferri/Work/build/src ../src/libckaapi.a /usr/lib/libpthread.dylib
Here's a guess at a workaround: try building the library with the -fno-common flag. If you have multiple definitions of this variable, you'll need to add "extern" to all but one.
Note that the following is a guess, and I can't say for certain unless/until you provide the exact compiler flags you're using -- but Xcode defaults to setting -fvisibility=hidden, which would hide pretty much any symbol in your library, unless it's declared as visible.
You can do the same on Linux, but GCC's default is not to hide symbols.
You'll find more information here: http://gcc.gnu.org/wiki/Visibility

Resources