clang - Cross compile for linux on macOS - c

I am trying to cross compile for linux on macOS. It appears that clang is not respecting the sysroot I set.
Program
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
Commands
CC=/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin/clang
${CC} -v -c main.c -target x86_64-pc-linux-elf -o main.o\
-isysroot /Users/zrgorak/Development/com/prog/site_scons/builders/linux/sysroot
${CC} -v -o main -target x86_64-pc-linux-elf main.o \
-isysroot /Users/zrgorak/Development/com/prog/site_scons/builders/linux/sysroot
Output
% make
/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin/clang -v -c -target x86_64-pc-linux-elf main.c -o main.o \
-isysroot /Users/user/Development/com/prog/site_scons/builders/linux/sysroot
clang version 9.0.0 (git://github.com/llvm/llvm-project.git 0399d5a9682b3cef71c653373e38890c63c4c365)
Target: x86_64-pc-linux-elf
Thread model: posix
InstalledDir: /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin
"/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/bin/clang-9" -cc1 -triple x86_64-pc-linux-elf -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -target-linker-version 512.4 -v -coverage-notes-file /Users/user/Development/testlinker/main.gcno -resource-dir /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/lib/clang/9.0.0 -isysroot /Users/user/Development/com/prog/site_scons/builders/linux/sysroot -internal-isystem /usr/local/include -internal-isystem /Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/lib/clang/9.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /Users/user/Development/testlinker -ferror-limit 19 -fmessage-length 128 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -faddrsig -o main.o -x c main.c
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-apple-darwin19.0.0
ignoring nonexistent directory "/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Users/user/Development/com/prog/site_scons/builders/darwin/clang+llvm-9.0.0-x86_64-darwin-apple/lib/clang/9.0.0/include
End of search list.
main.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
make: *** [target] Error 1
My sysroot is /usr and /lib copied from Ubuntu 19.x
% ll /Users/user/Development/com/prog/site_scons/builders/linux/sysroot/usr/include/stdio.h
-rw-r--r--# 1 user staff 29949 Sep 16 10:56 /Users/user/Development/com/prog/site_scons/builders/linux/sysroot/usr/include/stdio.h
This even happens when I use crosstool-ng and use their sysroot.
It's like clang doesn't look in the sysroot I set?

Related

Build .ko file from .c file using GCC command line

As a learning exercise, I'm trying to build a kernel object file from a c file which would only do printk("hello, world") when loaded with modprobe. My kernel version is 4.15.
So let's say I have this example code:
int printk(const char *fmt, ...);
struct platform_device;
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
};
static struct platform_driver my_driver = {
.probe = my_driver_probe,
.remove = my_driver_remove
};
module_platform_driver(my_driver);
static int my_driver_probe(struct platform_device *pdev) {
printk("my driver loaded\n");
return 0;
}
static int my_driver_remove(struct platform_device *pdev) {
printk("my driver removed\n");
return 0;
}
How do I compile it using gcc to create a valid my_driver.ko file that can be loaded with modprobe?
I'm trying to build a kernel object file from a c file which would only do printk("hello, world")
A short google search for such module resulted for me in the following examples: https://github.com/maK-/SimplestLKM https://github.com/moutoum/linux-kernel-module-hello-world https://github.com/ichergui/hello-world https://github.com/carloscdias/hello-world-linux-module .
How do I compile it using gcc to create a valid my_driver.ko file that can be loaded with modprobe?
The first project has the following source for a hello world module saved in hello.c:
#include<linux/init.h>
#include<linux/module.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("maK");
static int hello_init(void){
printk(KERN_ALERT "Hello world!\n");
return 0;
}
static void hello_exit(void){
printk(KERN_ALERT "Goodbye cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
The following is a transcript of a session with compiling the first project with V=1:
$ git clone https://github.com/maK-/SimplestLKM
Cloning into 'SimplestLKM'...
remote: Enumerating objects: 15, done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15
Receiving objects: 100% (15/15), 13.47 KiB | 431.00 KiB/s, done.
Resolving deltas: 100% (4/4), done.
$ cd SimplestLKM/
$ make V=1
make -C /lib/modules/5.10.72-1-lts/build M=/dev/shm/.1000.home.tmp.dir/SimplestLKM modules
make[1]: Entering directory '/usr/lib/modules/5.10.72-1-lts/build'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || ( \
echo >&2; \
echo >&2 " ERROR: Kernel configuration is invalid."; \
echo >&2 " include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
echo >&2 ; \
/bin/false)
make -f ./scripts/Makefile.build obj=/dev/shm/.1000.home.tmp.dir/SimplestLKM \
single-build= \
need-builtin=1 need-modorder=1
gcc -Wp,-MMD,/dev/shm/.1000.home.tmp.dir/SimplestLKM/.hello.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN -DMODULE -DKBUILD_BASENAME='"hello"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.c
./tools/objtool/objtool orc generate --module --no-fp --retpoline --uaccess /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o
{ echo /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o; echo; } > /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod
{ echo /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.ko; :; } | awk '!x[$0]++' - > /dev/shm/.1000.home.tmp.dir/SimplestLKM/modules.order
make -f ./scripts/Makefile.modpost
sed 's/ko$/o/' /dev/shm/.1000.home.tmp.dir/SimplestLKM/modules.order | scripts/mod/modpost -a -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/Module.symvers -e -i Module.symvers -N -T -
make -f ./scripts/Makefile.modfinal
gcc -Wp,-MMD,/dev/shm/.1000.home.tmp.dir/SimplestLKM/.hello.mod.o.d -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN -DMODULE -DKBUILD_BASENAME='"hello.mod"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.c
ld -r -m elf_x86_64 --build-id=sha1 -T scripts/module.lds -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.ko /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o; true
make[1]: Leaving directory '/usr/lib/modules/5.10.72-1-lts/build'
From that we know that we also need the generated hello.mod.c. The content for this module as generated by modpost program is as follows:
#include <linux/module.h>
#define INCLUDE_VERMAGIC
#include <linux/build-salt.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
BUILD_SALT;
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__section(".gnu.linkonce.this_module") = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
MODULE_INFO(depends, "");
MODULE_INFO(srcversion, "C7C2D304485DDC1C93263AE");
With both files in place, we can compile them with gcc, first to hello.o and hello.mod.o and then combine with ld. So these are the compiler flags needed to compile the module on my system:
( cd /usr/lib/modules/5.10.72-1-lts/build/ && gcc -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN -DMODULE -DKBUILD_BASENAME='"hello"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.c )
( cd /usr/lib/modules/5.10.72-1-lts/build/ && gcc -nostdinc -isystem /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include -I./arch/x86/include -I./arch/x86/include/generated -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -fmacro-prefix-map=./= -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Wno-format-security -std=gnu89 -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fcf-protection=none -m64 -falign-jumps=1 -falign-loops=1 -mno-80387 -mno-fp-ret-in-387 -mpreferred-stack-boundary=3 -mskip-rax-setup -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mindirect-branch=thunk-extern -mindirect-branch-register -fno-jump-tables -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=2048 -fstack-protector-strong -Wimplicit-fallthrough -Wno-unused-but-set-variable -Wno-unused-const-variable -g -gdwarf-4 -pg -mrecord-mcount -mfentry -DCC_USING_FENTRY -Wdeclaration-after-statement -Wvla -Wno-pointer-sign -Wno-stringop-truncation -Wno-zero-length-bounds -Wno-array-bounds -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -fplugin=./scripts/gcc-plugins/structleak_plugin.so -fplugin-arg-structleak_plugin-byref-all -DSTRUCTLEAK_PLUGIN -DMODULE -DKBUILD_BASENAME='"hello"' -DKBUILD_MODNAME='"hello"' -c -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.c )
( cd /usr/lib/modules/5.10.72-1-lts/build/ && ld -r -m elf_x86_64 --build-id=sha1 -T scripts/module.lds -o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.ko /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.o /dev/shm/.1000.home.tmp.dir/SimplestLKM/hello.mod.o )
After that we can insmod hello.ko and have hello world printed with CRIT in dmesg.
what are the compiler flags to use in order to do it.
In the same fashion as presented above, you can find out what options are passed to the compiler on your system.

Compiling gcc fails while trying to link libgcc

I am trying to build gcc for my linux. I'm cross compiling since I don't have a preexisting gcc.
I'm using musl as libc.
I configured gcc like this:
export CFLAGS="-pipe -march=native -fstack-protector-strong -fno-plt -pie -fpie"
../gcc-10.1.0/configure --target=x86_64-unknown-linux-musl \
--host=x86_64-unknown-linux-musl --build=x86_64-pc-linux-gnu \
--enable-lto --prefix=/usr --disable-nls --disable-multilib \
--enable-languages=c --without-isl --with-newlib --disable-libsanitizer \
--with-sysroot=/root/mnt
When I run make gcc compiles for a few minutes. After a while the build tries to execute the following
x86_64-unknown-linux-musl-gcc -O2 -g -O2 -pipe -march=native -fstack-protector-strong -fno-plt -pie -fpie -DIN_GCC -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -fpic -mlong-double-80 -DUSE_ELF_SYMVER -DGTHREAD_USE_WEAK=0 -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector -Dinhibit_libc -shared -nodefaultlibs -Wl,--soname=libgcc_s.so.1 -Wl,--version-script=libgcc.map -o ./libgcc_s.so.1.tmp -g -O2 -pipe -march=native -fstack-protector-strong -fno-plt -pie -fpie -B./ _muldi3_s.o _negdi2_s.o _lshrdi3_s.o _ashldi3_s.o _ashrdi3_s.o _cmpdi2_s.o _ucmpdi2_s.o _clear_cache_s.o _trampoline_s.o __main_s.o _absvsi2_s.o _absvdi2_s.o _addvsi3_s.o _addvdi3_s.o _subvsi3_s.o _subvdi3_s.o _mulvsi3_s.o _mulvdi3_s.o _negvsi2_s.o _negvdi2_s.o _ctors_s.o _ffssi2_s.o _ffsdi2_s.o _clz_s.o _clzsi2_s.o _clzdi2_s.o _ctzsi2_s.o _ctzdi2_s.o _popcount_tab_s.o _popcountsi2_s.o _popcountdi2_s.o _paritysi2_s.o _paritydi2_s.o _powisf2_s.o _powidf2_s.o _powixf2_s.o _mulhc3_s.o _mulsc3_s.o _muldc3_s.o _mulxc3_s.o _divhc3_s.o _divsc3_s.o _divdc3_s.o _divxc3_s.o _bswapsi2_s.o _bswapdi2_s.o _clrsbsi2_s.o _clrsbdi2_s.o _fixunssfsi_s.o _fixunsdfsi_s.o _fixunsxfsi_s.o _fixsfdi_s.o _fixdfdi_s.o _fixxfdi_s.o _fixunssfdi_s.o _fixunsdfdi_s.o _fixunsxfdi_s.o _floatdisf_s.o _floatdidf_s.o _floatdixf_s.o _floatundisf_s.o _floatundidf_s.o _floatundixf_s.o _divdi3_s.o _moddi3_s.o _divmoddi4_s.o _udivdi3_s.o _umoddi3_s.o _udivmoddi4_s.o _udiv_w_sdiv_s.o sfp-exceptions_s.o addtf3_s.o divtf3_s.o multf3_s.o negtf2_s.o subtf3_s.o unordtf2_s.o fixtfsi_s.o fixunstfsi_s.o floatsitf_s.o floatunsitf_s.o fixtfdi_s.o fixunstfdi_s.o floatditf_s.o floatunditf_s.o fixtfti_s.o fixunstfti_s.o floattitf_s.o floatuntitf_s.o extendsftf2_s.o extenddftf2_s.o extendxftf2_s.o trunctfsf2_s.o trunctfdf2_s.o trunctfxf2_s.o getf2_s.o letf2_s.o eqtf2_s.o _divtc3_s.o _multc3_s.o _powitf2_s.o enable-execute-stack_s.o unwind-dw2_s.o unwind-dw2-fde-dip_s.o unwind-sjlj_s.o unwind-c_s.o emutls_s.o libgcc.a -lc
in my build directory under x86_64-unknown-linux-musl/libgcc which fails with:
/root/mnt/tools/lib/gcc/x86_64-unknown-linux-musl/10.1.0/../../../../x86_64-unknown-linux-musl/bin/ld: /root/mnt/tools/lib/gcc/x86_64-unknown-linux-musl/10.1.0/../../../../x86_64-unknown-linux-musl/lib/Scrt1.o: in function `_start_c':
Scrt1.c:(.text._start_c+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
I verified with objdump -t crt1.o and objdump -t Scrt1.o that these object files define main, which they do.
Im pretty confused by now because the name libgcc suggests that it's a library, which of course does not have a main method. Why is ld expecting one? What is the build trying to do here?

Use TEMP_FAILURE_RETRY on OSX

I have some code utilizing TEMP_FAILURE_RETRY and I cannot build it. I have defined _GNU_SOURCE and included unistd.h in that order.
Running gcc -Wall -v archunix5a_2.c -o archunix5a_2 gives the following output:
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
"/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name archunix5a_2.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 274.2 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0 -Wall -fdebug-compilation-dir /Users/mateuszsadowski/Documents/programowanie/nauka/C/SOP/4 -ferror-limit 19 -fmessage-length 59 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/fp/qqgmt88x247bd712wbbw8wh80000gn/T/archunix5a_2-7cb96f.o -x c archunix5a_2.c
clang -cc1 version 8.0.0 (clang-800.0.42.1) default target x86_64-apple-darwin16.3.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0/include
/Library/Developer/CommandLineTools/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
archunix5a_2.c:113:13: warning: implicit declaration of
function 'TEMP_FAILURE_RETRY' is invalid in C99
[-Wimplicit-function-declaration]
if (TEMP_FAILURE_RETRY(fsync(fd)) == -1)
^
1 warning generated.
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o archunix5a_2 /var/folders/fp/qqgmt88x247bd712wbbw8wh80000gn/T/archunix5a_2-7cb96f.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_TEMP_FAILURE_RETRY", referenced from:
_cleanup in archunix5a_2-7cb96f.o
_main in archunix5a_2-7cb96f.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: *** [archunix5a_2] Error 1
I'd appreciate any help.
As i said earlier mac osx doesn't support most of the glib libraries. if you want to use TEMP_FAILURE_RETRY you can try the following
don't forget to include unistd. read more from here
here with this macro
#include <unistd.h> // for TEMP_FAILURE_RETRY
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(exp) \
({ \
decltype(exp) _rc; \
do { \
_rc = (exp); \
} while (_rc == -1 && errno == EINTR); \
_rc; \
})
#endif
i hope this helps

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.

Cross-Compiling with mingw for Win on Linux a pthread

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?

Resources