i try to compile a 32 bit app on my mac osx 64bit
I have a 32bit lib included.
I try to create a personal lib
gcc -m32 -c fileA.c -Iinclude -o fileA.o
gcc -m32 -c fileB.c -Iinclude -o fileB.o
All Ok
now I create .a File
ar ruv ./lib/myLib.a fileA.o fileB.o
When I try to launch
gcc -m32 -o imageMod imageMod.c -Iinclude -Llib
I receive the following error
Undefined symbols for architecture i386: "_addozzo", referenced
from:
_main in imageMod-nfyyGP.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1
(use -v to see invocation)
anyone can help me?
You need to tell gcc to link in the library:
gcc -m32 -o imageMod imageMod.c -Iinclude -Llib -l:myLib.a
Related
Attempting to compile dieharder from source on an M1 Max (macOS 12 + Xcode 13.3.1 + command line tools) and running into an error:
...
/bin/sh ../libtool --tag=CC --mode=link gcc -std=c99 -Wall -pedantic -I/usr/local/include -I/opt/local/include -L/opt/local/lib -o dieharder dieharder-add_ui_rngs.o dieharder-add_ui_tests.o dieharder-choose_rng.o dieharder-dieharder.o dieharder-dieharder_exit.o dieharder-help.o dieharder-list_rngs.o dieharder-list_tests.o dieharder-output.o dieharder-output_rnds.o dieharder-parsecl.o dieharder-rdieharder.o dieharder-run_all_tests.o dieharder-run_test.o dieharder-set_globals.o dieharder-testbits.o dieharder-time_rng.o dieharder-user_template.o ../libdieharder/libdieharder.la -lgsl -lgslcblas -lm -lgsl -lgslcblas
libtool: link: gcc -std=c99 -Wall -pedantic -I/usr/local/include -I/opt/local/include -o dieharder dieharder-add_ui_rngs.o dieharder-add_ui_tests.o dieharder-choose_rng.o dieharder-dieharder.o dieharder-dieharder_exit.o dieharder-help.o dieharder-list_rngs.o dieharder-list_tests.o dieharder-output.o dieharder-output_rnds.o dieharder-parsecl.o dieharder-rdieharder.o dieharder-run_all_tests.o dieharder-run_test.o dieharder-set_globals.o dieharder-testbits.o dieharder-time_rng.o dieharder-user_template.o -L/opt/local/lib ../libdieharder/.libs/libdieharder.a -lm -lgsl -lgslcblas
Undefined symbols for architecture arm64:
"_insert", referenced from:
_dab_filltree in libdieharder.a(libdieharder_la-dab_filltree.o)
_main_filltree in libdieharder.a(libdieharder_la-dab_filltree.o)
"_insertBit", referenced from:
_dab_filltree2 in libdieharder.a(libdieharder_la-dab_filltree2.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
...
I got to this failure with a MacPorts installed gsl and by executing the following:
$ mkdir dieharder; cd dieharder
$ wget --no-check-certificate https://webhome.phy.duke.edu/~rgb/General/dieharder/dieharder-3.31.1.tgz
$ tar xf dieharder-3.31.1.tgz
$ cd dieharder-3.31.1
$ sudo port install gsl
$ ./configure LDFLAGS=-L/opt/local/lib CPPFLAGS=-I/opt/local/include CFLAGS=-I/opt/local/include --disable-shared
...
$ make
...
I reproduced the failure with Homebrew:
$ brew install gsl
$ make distclean
$ ./configure LDFLAGS=-L/opt/homebrew/lib CPPFLAGS=-I/opt/homebrew/include CFLAGS=-I/opt/homebrew/include --disable-shared
...
$ make
...
/bin/sh ../libtool --tag=CC --mode=link gcc -std=c99 -Wall -pedantic -I/usr/local/include -I/opt/homebrew/include -L/opt/homebrew/lib -o dieharder dieharder-add_ui_rngs.o dieharder-add_ui_tests.o dieharder-choose_rng.o dieharder-dieharder.o dieharder-dieharder_exit.o dieharder-help.o dieharder-list_rngs.o dieharder-list_tests.o dieharder-output.o dieharder-output_rnds.o dieharder-parsecl.o dieharder-rdieharder.o dieharder-run_all_tests.o dieharder-run_test.o dieharder-set_globals.o dieharder-testbits.o dieharder-time_rng.o dieharder-user_template.o ../libdieharder/libdieharder.la -lgsl -lgslcblas -lm -lgsl -lgslcblas
libtool: link: gcc -std=c99 -Wall -pedantic -I/usr/local/include -I/opt/homebrew/include -o dieharder dieharder-add_ui_rngs.o dieharder-add_ui_tests.o dieharder-choose_rng.o dieharder-dieharder.o dieharder-dieharder_exit.o dieharder-help.o dieharder-list_rngs.o dieharder-list_tests.o dieharder-output.o dieharder-output_rnds.o dieharder-parsecl.o dieharder-rdieharder.o dieharder-run_all_tests.o dieharder-run_test.o dieharder-set_globals.o dieharder-testbits.o dieharder-time_rng.o dieharder-user_template.o -L/opt/homebrew/lib ../libdieharder/.libs/libdieharder.a -lm -lgsl -lgslcblas
Undefined symbols for architecture arm64:
"_insert", referenced from:
_dab_filltree in libdieharder.a(libdieharder_la-dab_filltree.o)
_main_filltree in libdieharder.a(libdieharder_la-dab_filltree.o)
"_insertBit", referenced from:
_dab_filltree2 in libdieharder.a(libdieharder_la-dab_filltree2.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [dieharder] Error 1
make: *** [dieharder.time] Error 2
Anyone know what might be going on here and how to properly configure this to complete compilation on and for Apple Silicon systems?
It turns out that the insert functions are not exported public for the ARM compilation of the library:
Undefined symbols for architecture arm64:
"_insert", referenced from:
_dab_filltree in libdieharder.a(libdieharder_la-dab_filltree.o)
_main_filltree in libdieharder.a(libdieharder_la-dab_filltree.o)
"_insertBit", referenced from:
_dab_filltree2 in libdieharder.a(libdieharder_la-dab_filltree2.o)
See: https://github.com/eddelbuettel/dieharder/pull/1/files
After adding static to those four lines dieharder compiles fine.
Screenshot of changes required:
I have 3 files, main.c, lists.c, and lists.h.
Im trying to write a Makefile with all the files are in the same directory:
maman21: lists.c lists.h main.c
gcc -g -Wall -ansi main.c -o maman21 -lm
going to the folder through terminal and using make shows me this message:
gcc -g -Wall -ansi main.c -o maman21 -lm
Undefined symbols for architecture x86_64: "_linkedListWay",
referenced from:
_main in main-C9dUT4.o "_reallocWay", referenced from:
_main in main-C9dUT4.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: *** [maman21] Error 1
reallocWay and linkedListWay are to functions I'm using in the file.
Thank you for your help.
You've failed to include the lists.c file in the compiler invocation so it doesn't get built.
It should be:
CFLAGS=-g -Wall -ansi
LDLIBS=-lm
maman21: main.o lists.o
main.o: main.c
lists.o: lists.c lists.h
The above uses implicit Makefile rules, it "knows" how to convert a C file to an object (.o) file.
Also, is it normal for Clang to be called gcc?
I have problem link libgcc into a static linked .so
it only happens when linking 64bit module with -m64
Ubuntu 64bit 12.10 gcc 4.7
also failed on Ubuntu 64bit 12.04 gcc 4.6
32bit no problem
$gcc -fPIC -c -o hello.o hello.c -m32
$gcc -shared -m32 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic -lc
$ ldd libhello.so
statically linked
64bit failed
$ make
gcc -fPIC -c -o hello.o hello.c
gcc -shared -m64 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic -lc
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a(iofclose.o): relocation R_X86_64_32 against `__gcc_personality_v0' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [libhello.so] Error 1
hello.c
#include <stdio.h>
int f(){
FILE *out = fopen("/tmp/x.log", "wb");
fclose(out);
return 1;
}
Makefile
all: libhello.so
libhello.so: hello.o
gcc -shared -m64 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic -lc
hello.o: hello.c
gcc -fPIC -c -o hello.o hello.c
clean:
rm -f hello.o libhello.so
The answer is basically "you can't do that." You're trying to link non-PIC code into a shared library, which is simply impossible on the x86_64 (amd64) architecture. You would need a static but PIC version of libgcc, and I suspect that would be only the start of your problems.
One of the reasons why libgcc is normally shared is that a given running executable has to have one and only one copy of some of the key data structures that libgcc maintains. Static linking makes sense for a final executable, since that one and only one copy will be the one statically linked into the executable, but the whole point of a dynamic object is to be loaded into another executable (which in turn will have its own copy of libgcc, either shared or static).
I have following simple program:
import std.stdio;
int main(string[] argv) {
writeln("Hello, world!");
return 0;
}
I'm building it as follows:
DMD -c -m64 -od/proj/out -w -wi -fPIC -debug \
-g -I/proj/hello -unittest /proj/hello.d
LD -L/usr/share/dmd/lib/ -arch x86_64 -execute -macosx_version_min 10.7 \
-pie -lm -lpthread -lphobos2 -o /proj/out/hello_app /proj/out/hello.o
Compilation passes perfectly, but linking stucks with following:
Undefined symbols for architecture x86_64:
"start", referenced from:
-u command line option
(maybe you meant: _D3std9algorithm41__T10startsWithVAyaa6_61203d3d2062TAhTAhZ10startsWithFAhAhZb, _D4core6thread6Thread5startMFZv , _D3std9algorithm91__T10startsWithVAyaa11_62203c20612e74696d6554TAS3std8datetime13PosixTimeZone10TransitionTlZ10startsWithFAS3std8datetime13PosixTimeZone10TransitionlZb , _D3std9algorithm43__T10startsWithVAyaa6_61203d3d2062TAyaTAyaZ10startsWithFAyaAyaZb , _D3std9algorithm41__T10startsWithVAyaa6_61203d3d2062TAxaTaZ10startsWithFAxaaZb , _D3std9algorithm92__T10startsWithVAyaa11_62203c20612e74696d6554TAS3std8datetime13PosixTimeZone10LeapSecondTylZ10startsWithFAS3std8datetime13PosixTimeZone10LeapSecondylZb , _D3std9algorithm92__T10startsWithVAyaa11_62203c20612e74696d6554TAS3std8datetime13PosixTimeZone10TransitionTylZ10startsWithFAS3std8datetime13PosixTimeZone10TransitionylZb )
ld: symbol(s) not found for architecture x86_64
I guess I forgot some additional static library to link with to have it setup everything, but what exactly?
Also I've seen instructions about how to do separate compilation and linking somewhere on dlang site, but cannot find it.
UPD1: When linking with help of GCC using gcc -L/usr/share/dmd/lib/ -lphobos2 -lm -lpthread hello.o, it works, but I need to use ld.
Add -lcrt1.o when linking.
LD -L/usr/share/dmd/lib/ -arch x86_64 -execute -macosx_version_min 10.7 \
-pie -lm -lpthread -lphobos2 -lcrt1.o -o /proj/out/hello_app /proj/out/hello.o
[update]
Ah, you got it : )
Found it due to pure luck!
It should be linked with -lphobos2 -lm -lpthread and -lcrt1.o - then everything links and works fine.
I had modified make file as
linux:
$(CC) mongoose.c -shared -fPIC -fpic -m32 -o $(LIB) $(LINFLAGS)
$(CC) mongoose.c main.c -m32 -o $(PROG) $(LINFLAGS)
But when I run ld on mongoose I gets. I am getting warning that it is incompatible with i386
ld mongoose
ld: warning: i386 architecture of input file `mongoose' is incompatible with i386:x86-64 output
ld is trying to create an x86-64 executable from your x86-32 object files, and is unhappy because that isn't something that makes sense. Use GCC to link instead of ld, passing the -m32 at link as well, and it will call the linker with the correct flags for linking x86-32 binaries.