Cross-Compiling with mingw for Win on Linux a pthread - c

I try do compile a simple thread program for Win on Linux with mingw.
For that I load the source and drop the source to my directory.
Than I wrote a makefile. For Linux it runs perfectly. But for Win I got many errors - some of them I can't fix already:
$ make
x86_64-w64-mingw32-gcc -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN -c -o main.o main.c
x86_64-w64-mingw32-gcc -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN -c -o usleep.o usleep.c
------------------------------------------
clean up
make clean
make[1]: Betrete Verzeichnis '/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Verlasse Verzeichnis '/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Betrete Verzeichnis '/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Betrete Verzeichnis '/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: Erzeugen von libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Verlasse Verzeichnis '/mnt/prog/pthreads'
make[1]: Verlasse Verzeichnis '/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN
------------------------------------------
create object files
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
./usleep.o:usleep.c:(.text+0x8c): undefined reference to `__imp_select'
/usr/bin/x86_64-w64-mingw32-ld: ./usleep.o: Falsche Verschiebungsadresse 0x0 in Abschnitt `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Ungültiger Vorgang
collect2: ld returned 1 exit status
make: *** [prog] Fehler 1
How can I fix the linking problem?
to Dayal rai
the includes of my headers are (in the order, which I think they are called):
main.c
/*#include <errno.h> */
/*#include <fcntl.h> */
#include <inttypes.h>
#include "./pthreads/pthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <termios.h> */
/*#include <unistd.h> */
#include "usleep.h" /* um usleep nutzen zu können */
usleep.h
...
#include "unistd.h"
#include <inttypes.h>
#include <stdio.h>
...
Could it be that it is wrong to include unistd.h for a windows target?
I additionally reduce the number of headers and change their orders in any possibility, but the error still exist. I don't know where it come from....
main.c
#include <inttypes.h> /* Spezielle int-Datentypen */
#include <stdio.h>
#if defined(FORCEWIN) || defined(_WIN64) || defined(_WIN32)
#include <time.h>
#include <winsock2.h>
#elif __unix /* all unices not caught above */
#include <sys/time.h>
#include <sys/socket.h>
#endif
usleep.h
with english feedback
LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/pthreads'
make[1]: Leaving directory `/mnt/prog/pthreads'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
./usleep.o:usleep.c:(.text+0x8c): undefined reference to `__imp_select'
/usr/bin/x86_64-w64-mingw32-ld: ./usleep.o: bad reloc address 0x0 in section `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
make: *** [prog] Error 1
I have remove -DPTW32_STATIC_LIB the from the CFLAGS and now I get this
LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/pthreads'
make[1]: Leaving directory `/mnt/prog/pthreads'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -FORCEWIN
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe -v
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-w64-mingw32/4.6/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../../src/configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/include' --mandir='/usr/share/man' --infodir='/usr/share/info' --sysconfdir=/etc --localstatedir=/var --libexecdir='/usr/lib/gcc-mingw-w64' --disable-maintainer-mode --disable-dependency-tracking --prefix=/usr --enable-shared --enable-static --disable-multilib --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --libdir=/usr/lib --enable-libstdcxx-time=yes --with-tune=generic --enable-version-specific-runtime-libs --enable-threads=win32 --enable-fully-dynamic-string --enable-sjlj-exceptions --enable-languages=c,c++,fortran,objc,obj-c++,ada --enable-lto --with-plugin-ld --target=x86_64-w64-mingw32 --with-gxx-include-dir=/usr/include/c++/4.6 --with-as=/usr/bin/x86_64-w64-mingw32-as --with-ld=/usr/bin/x86_64-w64-mingw32-ld
Thread model: win32
gcc version 4.6.3 (GCC)
COMPILER_PATH=/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/
LIBRARY_PATH=/usr/lib/gcc/x86_64-w64-mingw32/4.6/:/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/
COLLECT_GCC_OPTIONS='-o' 'possix.exe' '-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-w64-mingw32/4.6/collect2 -m i386pep -Bdynamic -o possix.exe /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/4.6 -L/usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib ./main.o ./usleep.o ./pthreads/pthread.o -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/x86_64-w64-mingw32/4.6/../../../../x86_64-w64-mingw32/lib/crtend.o
./main.o:main.c:(.text+0x1c): undefined reference to `__imp_pthread_mutex_lock'
./main.o:main.c:(.text+0x23): undefined reference to `__imp_pthread_mutex_unlock'
./main.o:main.c:(.text+0x6b): undefined reference to `__imp_pthread_exit'
./main.o:main.c:(.text+0x8b): undefined reference to `__imp_pthread_mutex_lock'
./main.o:main.c:(.text+0x9e): undefined reference to `__imp_pthread_mutex_unlock'
./main.o:main.c:(.text+0x19c): undefined reference to `__imp_pthread_exit'
/usr/bin/x86_64-w64-mingw32-ld: ./main.o: bad reloc address 0x0 in section `.pdata'
/usr/bin/x86_64-w64-mingw32-ld: final link failed: Invalid operation
collect2: ld returned 1 exit status
make: *** [prog] Error 1
It looks to me like a error coming from pthread confused
In reference to the comments by Gavin Smith below I have done some changes in the makefile:
$ LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog'
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -g -O3 -FORCEWIN
usleep.o main.o
------------------------------------------
linking
x86_64-w64-mingw32-ld ./main.o ./usleep.o ./pthreads/libpthreadGC2.a -o possix.exe -v
GNU ld (GNU Binutils) 2.22.90.20120919
./main.o: In function `thr_ausgabe':
/mnt/prog/./main.c:66: undefined reference to `__imp_pthread_mutex_lock'
/mnt/prog/./main.c:66: undefined reference to `__imp_pthread_mutex_unlock'
/mnt/prog/./main.c:72: undefined reference to `printf'
/mnt/prog/./main.c:83: undefined reference to `__imp_pthread_exit'
./main.o: In function `thr_arbeite':
/mnt/prog/./main.c:44: undefined reference to `__imp_pthread_mutex_lock'
/mnt/prog/./main.c:46: undefined reference to `__imp_pthread_mutex_unlock'
/mnt/prog/./main.c:56: undefined reference to `__imp_pthread_exit'
./main.o: In function `main':
/mnt/prog/./main.c:89: undefined reference to `__main'
/mnt/prog/./main.c:100: undefined reference to `__imp_pthread_mutex_init'
/mnt/prog/./main.c:105: undefined reference to `__imp_pthread_create'
/mnt/prog/./main.c:116: undefined reference to `__imp_pthread_join'
/mnt/prog/./main.c:111: undefined reference to `__imp___iob_func'
/mnt/prog/./main.c:111: undefined reference to `fprintf'
/mnt/prog/./main.c:106: undefined reference to `__imp___iob_func'
/mnt/prog/./main.c:106: undefined reference to `fprintf'
./usleep.o: In function `msleep':
/mnt/prog/./usleep.c:16: undefined reference to `__imp_select'
./usleep.o: In function `usleep':
/mnt/prog/./usleep.c:33: undefined reference to `__imp_select'
make: *** [prog] Error 1
The symbols of the main are now:
$ x86_64-w64-mingw32-nm main.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_frame
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_loc
0000000000000000 N .debug_ranges
U fprintf
U __imp___iob_func
U __imp_pthread_create
U __imp_pthread_exit
U __imp_pthread_join
U __imp_pthread_mutex_init
U __imp_pthread_mutex_lock
U __imp_pthread_mutex_unlock
0000000000000000 T main
U __main
U msleep
0000000000000000 p .pdata
0000000000000000 p .pdata.startup
U printf
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 t .text.startup
0000000000000080 T thr_arbeite
0000000000000000 T thr_ausgabe
0000000000000000 r .xdata
0000000000000000 r .xdata.startup
I looking in the objectfile of the lib too an found the symbols a bit different to the main:
000000000000a9c0 T pthread_create
000000000000e6a0 T pthread_exit
000000000000b510 T pthread_join
00000000000024b0 T pthread_mutex_init
0000000000006c70 T pthread_mutex_lock
0000000000005800 T pthread_mutex_unlock
could it be useful to strip or rename the symbols from object files?
The actual situation:
terminal:
$ LC_ALL=C make
------------------------------------------
clean up
make clean
make[1]: Entering directory `/mnt/prog/win'
rm -f *.o *.obj *.exe
make[1]: Leaving directory `/mnt/prog/win'
------------------------------------------
create object files
x86_64-w64-mingw32-gcc -c ./main.c ./usleep.c -Wall -O3 -DPTW32_STATIC_LIB -FORCEWIN
usleep.o main.o
------------------------------------------
compile pthreads as static lib
cd ./pthreads/; \
make CROSS=x86_64-w64-mingw32- clean GC-static
make[1]: Entering directory `/mnt/prog/win/pthreads'
rm -f *~
rm -f *.i
rm -f *.s
rm -f *.o
rm -f *.obj
rm -f *.exe
rm -f pthread.def
make XOPT="-DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB" CLEANUP=-D__CLEANUP_C XC_FLAGS=" " OBJ="pthread.o version.o" libpthreadGC2.stamp
make[2]: Entering directory `/mnt/prog/win/pthreads'
x86_64-w64-mingw32-gcc -c -o pthread.o -D__CLEANUP_C -O3 -DPTW32_BUILD_INLINED -DPTW32_STATIC_LIB -I. -DHAVE_PTW32_CONFIG_H -Wall pthread.c
x86_64-w64-mingw32-windres --include-dir=. -D__CLEANUP_C -o version.o -i version.rc
rm -f libpthreadGC2.a
x86_64-w64-mingw32-ar -rv libpthreadGC2.a pthread.o version.o
x86_64-w64-mingw32-ar: creating libpthreadGC2.a
a - pthread.o
a - version.o
x86_64-w64-mingw32-ranlib libpthreadGC2.a
echo touched > libpthreadGC2.stamp
make[2]: Leaving directory `/mnt/prog/win/pthreads'
make[1]: Leaving directory `/mnt/prog/win/pthreads'
------------------------------------------
linking
x86_64-w64-mingw32-ld ./main.o ./usleep.o ./pthreads/libpthreadGC2.a -o possix.exe
./main.o:main.c:(.text+0x33): undefined reference to `printf'
./main.o:main.c:(.text.startup+0x6): undefined reference to `__main'
x86_64-w64-mingw32-ld: ./main.o: bad reloc address 0x6 in section `.text.startup'
x86_64-w64-mingw32-ld: final link failed: Invalid operation
make: *** [prog] Error 1
the symbols of the object file
$ x86_64-w64-mingw32-nm main.o
0000000000000000 b .bss
0000000000000000 d .data
U fprintf
U __imp___iob_func
0000000000000000 T main
U __main
U msleep
0000000000000000 p .pdata
0000000000000000 p .pdata.startup
U printf
U pthread_create
U pthread_exit
U pthread_join
U pthread_mutex_init
U pthread_mutex_lock
U pthread_mutex_unlock
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 t .text.startup
0000000000000080 T thr_arbeite
0000000000000000 T thr_ausgabe
0000000000000000 r .xdata
0000000000000000 r .xdata.startup
I really don't understand why he can't find printf and main....
usleep.o
$ x86_64-w64-mingw32-nm usleep.o
0000000000000000 b .bss
0000000000000000 d .data
U __imp_select
0000000000000000 T msleep
0000000000000000 p .pdata
0000000000000000 t .text
0000000000000050 T usleep
0000000000000000 r .xdata

The error:
./usleep.o:usleep.c:(.text+0x3c): undefined reference to `__imp_select'
means that the symbol __imp_select is not defined. It could be in some other C file that you did not include in your final link step.

It looks like you have created a static library file libpthreadGC2.a, but your linking command x86_64-w64-mingw32-gcc ./main.o ./usleep.o ./pthreads/pthread.o -o possix.exe -v only links three object files and it is not among them. Try adding -lpthreadGC2 or libpthreadGC2.a to the linking command.
If you want to check if an object file contains a missing symbol (e.g. __imp_pthread_mutex_unlock), you can try running nm libpthreadGC2.a | grep '__imp_pthread_mutex_unlock' to see if it is there.
I read in the README for the pthread sources you linked to that you have to define PTW32_STATIC_LIB when building - is there a reason you removed the -DPTW32_STATIC_LIB option?

Related

c function of my shared lib is not seen out of the lib

I have developed this so lib:
mylib.c:
#include "mylib.h"
int foo(int a,int b) {
return 1;
}
mylib.h:
#include <stdio.h>
extern int foo(int a,int b);
Makefile (libmylib.so is generated successfully in lib folder):
CFLAGS += -g -Wall -Werror -Wpointer-arith -fPIC -fno-strict-aliasing
OBJECTS = mylib.o
MY_LIBRARY := libmylib.so
all: $(MY_LIBRARY)
$(MY_LIBRARY): $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -fPIC -rdynamic -shared -Wl,-soname,$# -o ./lib/$# $^
$(OBJECTS):
$(CC) $(CFLAGS) $(INCLUDES) -c ${#:.o=.c} -o $#
clean :
#rm -f *.o
#rm -f ./lib/$(MY_LIBRARY)
my test program:
test.c:
#include <stdio.h>
#include <mylib.h> //mylib.h was copied to /usr/include
int main () {
foo(5,2);
return 0;
}
Compile:
gcc -c test.c -o test.o => pass
gcc -L./lib -lmylib test.o -o TEST => fail
ERROR:
user~/Desktop/test_lib$ gcc -L./lib -lmylib test.o -o TEST
test.o: In function `main':
test.c:(.text+0xf): undefined reference to `foo'
collect2: error: ld returned 1 exit status
I don't understand why it fails when generating my program ?
If you use -I option on the gcc command line, you don't need to pollute /usr/include with test files:
gcc -c test.c -o test.o -I.
The order of the files on the linking command line is important. Put libraries after the file which use them:
$ gcc -L./lib -lmylib test.o -o TEST
/usr/bin/ld: test.o: in function `main':
test.c:(.text+0x13): undefined reference to `foo'
collect2: error: ld returned 1 exit status
$ gcc test.o -L./lib -lmylib -o TEST
$
Then, use LD_LIBRARY_PATH environment variable to add the pathname of your library in the list of search paths of the dynamic linker (it is advised to use an absolute pathname but for test purposes, I use a relative one):
$ ./TEST
./TEST: error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory
$ ldd TEST
linux-vdso.so.1 (0x00007ffeb8bf1000)
libmylib.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f268fe40000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2690056000)
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib
$ ldd TEST
linux-vdso.so.1 (0x00007ffd75bcb000)
libmylib.so => ./lib/libmylib.so (0x00007f16ff112000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f16fef03000)
/lib64/ld-linux-x86-64.so.2 (0x00007f16ff11e000)
$ ./TEST
$
I have fixed this error with just change the order of options in gcc command when linking to mylib, I haven't an explanation but this fixed my issue.
user:~/Desktop/test_lib$ gcc -L./lib -lmylib test.o -o TEST
test.o: In function `main':
test.c:(.text+0xf): undefined reference to `foo'
collect2: error: ld returned 1 exit status
user:~/Desktop/test_lib$ gcc -L./lib test.o -o TEST -lmylib
user:~/Desktop/test_lib$ ls -l
total 40
drwxr-xr-x 2 user group 4096 nov. 16 19:15 lib
-rw-r--r-- 1 user group 346 nov. 16 19:00 Makefile
-rw-r--r-- 1 user group 54 nov. 16 18:20 mylib.c
-rw-r--r-- 1 user group 22 nov. 16 18:20 mylib.h
-rw-r--r-- 1 user group 2632 nov. 16 19:15 mylib.o
-rwxr-xr-x 1 user group 8592 nov. 17 14:20 TEST
-rw-r--r-- 1 user group 56 nov. 16 18:21 test.c
-rw-r--r-- 1 v 1376 nov. 16 19:15 test.o
user:~/Desktop/test_lib$

/usr/bin/ld: cannot find -lioutil in Makefile

I want to do makefile as the following, but I received this error:
gcc frparse.o frtags.o frscan.o frinit.o frstop.o frfoot.o frio.o -L/usr/local/image/lib/sgi -lioutil -lutil -o frparse
/usr/bin/ld: cannot find -lioutil
collect2: error: ld returned 1 exit status
Makefile:17: recipe for target 'frparse' failed
make: *** [frparse] Error 1
I would appreciate it if you guide me.
IMLIB = /usr/local/image/lib/sgi
LLIBS = -lioutil -lutil
it: frparse frcheck pgrep
OBJ = frparse.o frtags.o frscan.o frinit.o frstop.o frfoot.o frio.o
frparse.o: Makefile frparse.h frio.h frproto.h frstop.h frparse.c
frinit.o: Makefile frparse.h frproto.h frinit.c
frscan.o: Makefile frio.h frstop.h frscan.c
frfoot.o: Makefile frparse.h frproto.h frstop.h frfoot.c
frtags.o: Makefile frparse.h frio.h frproto.h frstop.h frtags.c
frstop.o: Makefile frstop.h frstop.c
frio.o: Makefile frio.c
frparse: $(OBJ)
gcc $(OBJ) -L$(IMLIB) $(LLIBS) -o $#
frcheck: frcheck.o
gcc frcheck.o -L$(IMLIB) $(LLIBS) -o $#
pgrep: pgrep.o
gcc pgrep.o -L$(IMLIB) $(LLIBS) -o $#
.c.o:
# cc -c -O2 -mips2 $<
gcc -c -g $<
regarding:
.c.o:
# cc -c -O2 -mips2 $<
gcc -c -g $<
The comment # must ALSO be indented via a <tab> otherwise the recipe never performs the third line
Also, the syntax is a bit obsolete suggest:
%.o:%.c
for the first line of the recipe
However, regarding your question:
Is there actually a library file named libioutil.so in the directory:
/usr/local/image/lib/sgi

compling glibc: /glibc-2.7/w/elf/ld.so: No such file or directory

I have to install a software in the directory of the supercomputer center, but I cannot use sudo, root, even apt-get, yum... The software requires glibc2.7, so I have to manually install both gcc and glibc (since I cannot change the path for built-in gcc to find the glibc).
The gcc seems to be installed successfully:
[geosign#node117 ~/binutils/bin]$ gcc -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: /public/home/geosign/software/gcc/configure -prefix=/public/home/geosign/gcc --with-gmp=/public/home/geosign/Gcc --with- mpfr=/public/home/geosign/Gcc --with-mpc=/public/home/geosign/Gcc --with-ppl=/public/home/geosign/ppl --with-cloog=/public/home/geosign/ppl --enable-threads=posix CFLAGS=-I/public/home/geosign/ppl/lib:/public/home/geosign/Gcc/lib CPPFLAGS=-I/public/home/geosign/ppl/lib:/public/home/geosign/Gcc/lib CXXFLAGS=-I/public/home/geosign/ppl/lib:/public/home/geosign/Gcc/lib --enable-languages=c,c++,fortran --disable-multilib
Thread model: posix
gcc version 4.4.7 (GCC)
However, when I use "make" to compile the glibc, it shows:
make[3]: Leaving directory `/public/home/geosign/software/glibc/elf'
gcc -B/public/home/geosign/binutils/ -nostdlib -nostartfiles -r -o /public/home/geosign/software/build/glibc/elf/librtld.os '-Wl,-(' /public/home/geosign/software/build/glibc/elf/dl-allobjs.os /public/home/geosign/software/build/glibc/elf/rtld-libc.a -lgcc '-Wl,-)' \
-Wl,-Map,/public/home/geosign/software/build/glibc/elf/librtld.os.map
gcc -B/public/home/geosign/binutils/ -nostdlib -nostartfiles -shared \
-Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both -Wl,-z,defs -Wl,--verbose 2>&1 | \
LC_ALL=C \
sed -e '/^=========/,/^=========/!d;/^=========/d' \
-e 's/\. = 0 + SIZEOF_HEADERS;/& _begin = . - SIZEOF_HEADERS;/' \
> /public/home/geosign/software/build/glibc/elf/ld.so.lds
gcc -B/public/home/geosign/binutils/ -nostdlib -nostartfiles -shared -o /public/home/geosign/software/build/glibc/elf/ld.so \
-Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both -Wl,-z,defs \
/public/home/geosign/software/build/glibc/elf/librtld.os -Wl,--version-script=/public/home/geosign/software/build/glibc/ld.map \
-Wl,-soname=ld-linux-x86-64.so.2 -T /public/home/geosign/software/build/glibc/elf/ld.so.lds
/public/home/geosign/software/build/glibc/elf/librtld.os: In function `_dl_start_final':
/public/home/geosign/software/glibc/elf/rtld.c:290: undefined reference to `_begin'
/public/home/geosign/binutils/bin/ld: /public/home/geosign/software/build/glibc/elf/librtld.os: relocation R_X86_64_PC32 against undefined hidden symbol `_begin' can not be used when making a shared object
/public/home/geosign/binutils/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
make[2]: *** [/public/home/geosign/software/build/glibc/elf/ld.so] Error 1
make[2]: Leaving directory `/public/home/geosign/software/glibc/elf'
make[1]: *** [elf/subdir_lib] Error 2
make[1]: Leaving directory `/public/home/geosign/software/glibc'
make: *** [all] Error 2
These sentences in Makefile of elf are:
$(objpfx)ld.so: $(objpfx)librtld.os $(ld-map)
#rm -f $#.lds
$(LINK.o) -nostdlib -nostartfiles -shared $(z-now-$(bind-now)) \
$(LDFLAGS-rtld) -Wl,-z,defs -Wl,--verbose 2>&1 | \
LC_ALL=C \
sed -e '/^=========/,/^=========/!d;/^=========/d' \
-e 's/\. = 0 + SIZEOF_HEADERS;/& _begin = . - SIZEOF_HEADERS;/' \
> $#.lds
$(LINK.o) -nostdlib -nostartfiles -shared -o $# \
$(LDFLAGS-rtld) -Wl,-z,defs $(z-now-$(bind-now)) \
$(filter-out $(map-file),$^) $(load-map-file) \
-Wl,-soname=$(rtld-installed-name) -T $#.lds
rm -f $#.lds
readelf -s $# \
| awk '($$7 ~ /^UND(|EF)$$/ && $$1 != "0:" && $$4 != "REGISTER") { print; p=1 } END { exit p != 0 }'
This is how I configure glibc:
/public/home/geosign/software/glibc/configure --prefix=/public/home/geosign/glibc --with-binutils=/public/home/geosign/binutils/ --enable-shared --disable-multilib
ldd ld shows:
[geosign#node117 ~/binutils/bin]$ ldd ld
linux-vdso.so.1 => (0x00007fff163be000)
libz.so.1 => /lib64/libz.so.1 (0x0000003fd1400000)
libc.so.6 => /lib64/libc.so.6 (0x0000003fd0400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003fd0000000)
Thank you very much. There is really limited time for me, and I have spent a lot of days on it.
Your immediate problem is this:
gcc -nostdlib -nostartfiles -shared -o -mcmodel=large \
/public/home/geosign/software/glibc-2.7/w/elf/ld.so ...
This tells GCC to create an output file named -mcmodel=large, instead of intended output file /public/home/geosign/software/glibc-2.7/w/elf/ld.so.
You didn't tell us how you've configured this GLIBC build, but I suspect you've used -mcmodel=large somewhere in ./configure invocation. Don't do that, there is no reason to use -mcmodel=large when building glibc.

Trying to use matlab's libmat.dll, but the compiler doesn't recognize the function from the library

I am trying to use matlab libmat.dll in a C application. To compile my C application I use MinGW, for now I use matlab exemple "matcreate.c" and try to compile it, so the projects consist of only one file : main.c .
Here is the makefile I use :
MATINCLUDE = "C:\Program Files\MATLAB\R2010a\extern\include"
MATLIBRARY = "C:\Program Files\MATLAB\R2010a\bin\win64"
#
CC = gcc
LD = gcc
CFLAGS = -O3 -Wall
LFLAGS = -Wall -O3
LIBS = -I$(MATINCLUDE) -L$(MATLIBRARY)
#
PROG = matTest
LISTEOBJ = \
main.o
.c.o :
$(CC) -c $(CFLAGS) $(LIBS) -o $# $<
all : $(PROG)
$(PROG) : $(LISTEOBJ)
$(LD) -o $(PROG) $(LFLAGS) $(LISTEOBJ) $(LIBS)
clean :
rm -f *.obj
Here is what I get in the console
E:\Users\Desk\Dropbox\matTest>make
gcc -c -O3 -Wall -I"C:\Program Files\MATLAB\R2010a\extern\include" -L"C:\Pr
ogram Files\MATLAB\R2010a\bin\win64" -o main.o main.c
gcc -o Hello_world -Wall -O3 main.o -I"C:\Program Files\MATLAB\R2010a\extern\i
nclude" -L"C:\Program Files\MATLAB\R2010a\bin\win64"
main.o:main.c:(.text.startup+0x48): undefined reference to `matOpen'
main.o:main.c:(.text.startup+0x6e): undefined reference to `mxCreateDoubleMatrix
_730'
e:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: main.o: ba
d reloc address 0x6e in section `.text.startup'
e:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
make: *** [Hello_world] Error 1
Why do I have "undefined reference to matOpen'" and "undefined reference to mxCreateDoubleMatrix" ?? those function are declared in mat.h. and I added #include "mat.h" to the begining of main.c
thank you
Looks like you have included the path to the matlab library, but not the library itself. You need to add a -l<libraryname> to your link line.

gcc compiler in cygwin not finding X window directory

I'm trying to compile a project that includes various files from X11 in my /usr/include directory. It compiles fine on a remote linux machine, but when I attempt to run the make file I get the following error:
$ make clean; make
rm -f xrobot.o mobile_base.o arm.o eye.o object.o 4D_math.o Xkw/Canvas.o Xkw/Sli
der.o Xkw/Xkw.o lib/simulator.a *~
cd ./Xkw; make; cd ..; \
make[1]: Entering directory `C:/cygwin/home/Gene/roger/mmRogerSpring2014/RogerSi
mulator/Xkw'
gcc -c -g -I. -I/usr/include/X11 -I/usr/X11R6/include Canvas.c
Canvas.c:9:22: fatal error: X11/Xos.h: No such file or directory
compilation terminated.
make[1]: *** [Canvas.o] Error 1
make[1]: Leaving directory `C:/cygwin/home/Gene/roger/mmRogerSpring2014/RogerSim
ulator/Xkw'
gcc -c -g -I. -I/usr/X11R6/include -I/usr/include/X11 -I./include xrobot.c
In file included from xrobot.c:11:0:
Xkw/Xkw.h:12:27: fatal error: X11/Intrinsic.h: No such file or directory
compilation terminated.
make: *** [xrobot.o] Error 1
However, I know for a fact that the files are there, I can do
$ cat /usr/include/X11/Xos.h
And read the file.
Here is my make file:
# replace this with the top of your X11 tree
# X11 = /exp/rcf/share/X11R5
X11 = /usr/X11R6
############## do not change below this line ####################
ROGERINCDIR = ./include
XINCDIR = $(X11)/include
XLIBDIR = $(X11)/lib
EDLAB_XINCDIR = /usr/include/X11
XAWLIB = -lXaw
XMULIB = -lXmu
XTOOLLIB = -lXt
XLIB = -lX11
XEXTLIB = -lXext
MATHLIB = -lm
LIBS = -L$(XLIBDIR) $(XAWLIB) $(XMULIB) $(XTOOLLIB) $(XLIB) $(XEXTLIB) \
$(MATHLIB)
RM = rm -f
CC = gcc
#CCFLAGS = -c -O $(OPT) -I. -I$(XINCDIR)
CCFLAGS = -c -g -I. -I$(XINCDIR) -I$(EDLAB_XINCDIR) -I$(ROGERINCDIR)
.SUFFIXES: .c .o
.c.o:
$(CC) $(CCFLAGS) $<
############## do not change above this line ####################
ROGERLIB = lib/simulator.a
OFILES1 = xrobot.o \
mobile_base.o \
arm.o \
eye.o \
object.o \
4D_math.o
XKWOFILES = Xkw/Canvas.o Xkw/Slider.o Xkw/Xkw.o
#XKWOFILES = Xkw/Canvas.o Xkw/Xkw.o
HFILES = Roger.h simulate.h control.h modes.h
all: subdirs $(OFILES1) $(ROGERLIB)
subdirs:
cd ./Xkw; make; cd ..; \
#$(ROGERLIB): $(OFILES1)
# $(CC) -o $# $(OFILES1) $(XKWOFILES) $(LIBS)
$(ROGERLIB): $(OFILES1) $(XKWOFILES)
ar r $(ROGERLIB) $(OFILES1) $(XKWOFILES)
clean:
$(RM) $(OFILES1) $(XKWOFILES) $(ROGERLIB) *~
#mobile_base.o: Roger.h simulate.h control.h
#arm.o: Roger.h simulate.h control.h
#eye.o: Roger.h simulate.h control.h
#object.o: Roger.h simulate.h control.h
#xrobot.o: Xkw/Xkw.h Roger.h simulate.h control.h
#tele_interface.o: Roger.h simulate.h control.h modes.h
Edit: I've changed the X11 variable at the top of the make file to be /usr/include instead of /usr/X11R6. It made no difference.
Edit 2: Here's the error with the above mentioned alternative include path.
$ make clean; make
rm -f xrobot.o mobile_base.o arm.o eye.o object.o 4D_math.o Xkw/Canvas.o Xkw/Sli
der.o Xkw/Xkw.o lib/simulator.a *~
cd ./Xkw; make; cd ..; \
make[1]: Entering directory `C:/cygwin/home/Gene/roger/mmRogerSpring2014/RogerSi
mulator/Xkw'
gcc -c -g -I. -I/usr/include/X11 -I/usr/X11R6/include Canvas.c
Canvas.c:9:22: fatal error: X11/Xos.h: No such file or directory
compilation terminated.
make[1]: *** [Canvas.o] Error 1
make[1]: Leaving directory `C:/cygwin/home/Gene/roger/mmRogerSpring2014/RogerSim
ulator/Xkw'
gcc -c -g -I. -I/usr/include/include -I/usr/include/X11 -I./include xrobot.c
In file included from xrobot.c:11:0:
Xkw/Xkw.h:12:27: fatal error: X11/Intrinsic.h: No such file or directory
compilation terminated.
make: *** [xrobot.o] Error 1
You should include X11/Xos.h as:
#include <X11/Xos.h>
If you include it as:
#include "X11/Xos.h"
it will be searched for relative to the source file, not in the -I directories.
That may be the issue.

Resources