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
Related
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?
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.
After finishing installing MinGW and GCC on my system, I decided to test it out. I wrote the following 3 files:
complex.h
# ifndef hjp_complex
# define hjp_complex
typedef struct _complex
{
double real, image;
} complex;
complex complex_add( const complex a, const complex b );
complex complex_sub( const complex a, const complex b );
complex complex_mul( const complex a, const complex b );
# endif
complex.c
# include "complex.h"
complex complex_add( const complex a, const complex b ) {
complex c = a;
c.real += b.real;
c.image += b.image;
return c;
}
complex complex_sub( const complex a, const complex b ) {
complex c = a;
c.real -= b.real;
c.image -= b.image;
return c;
}
complex complex_mul( const complex a, const complex b ) {
complex c = a;
c.real = a.real * b.real - a.image * b.image;
c.image = a.real * b.image + a.image * b.real;
return c;
}
testing.c
# include <stdio.h>
# include "complex.h"
# define cmplx_out(t) printf( "%.2f + %.2fi\n", t.real, t.image )
int main() {
complex x, g, h;
x.real = 3; h.real = 12;
x.image = 12; h.image = 3;
cmplx_out( x );
cmplx_out( h );
return 0;
}
After that, compiling and executing testing.c worked fine:
C:\Users\hjpotter92\Documents\Programming>gcc testing.c
C:\Users\hjpotter92\Documents\Programming>a
3.00 + 12.00i
12.00 + 3.00i
Now, when testing the functions for complex number computations (new testing.c):
# include <stdio.h>
# include "complex.h"
# define cmplx_out(t) printf( "%.2f + %.2fi\n", t.real, t.image )
int main() {
complex x, g, h;
x.real = 3; h.real = 12;
x.image = 12; h.image = 3;
g = complex_mul( x, h );
cmplx_out( g );
g = complex_sub( x, h );
cmplx_out( g );
g = complex_add( x, h );
cmplx_out( g );
return 0;
}
And when I try to compile it:
C:\Users\hjpotter92\Documents\Programming>gcc testing.c
C:\Users\HJPOTT~1\AppData\Local\Temp\ccwD6Lqh.o:testing.c:(.text+0x7e): undefined reference to `complex_mul'
C:\Users\HJPOTT~1\AppData\Local\Temp\ccwD6Lqh.o:testing.c:(.text+0xe8): undefined reference to `complex_sub'
C:\Users\HJPOTT~1\AppData\Local\Temp\ccwD6Lqh.o:testing.c:(.text+0x172): undefined reference to `complex_add'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\HJPOTT~1\AppData\Local\Temp\ccwD6Lqh.o: bad reloc address 0x20 in section `.eh_frame'
c:/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
Referring to a few other questions, I tried a lot of combinations:
compiling both of them
C:\Users\hjpotter92\Documents\Programming>gcc complex.c testing.c
C:\Users\HJPOTT~1\AppData\Local\Temp\ccKuURyc.o:testing.c:(.text+0x7e): undefined reference to `complex_mul'
C:\Users\HJPOTT~1\AppData\Local\Temp\ccKuURyc.o:testing.c:(.text+0xe8): undefined reference to `complex_sub'
C:\Users\HJPOTT~1\AppData\Local\Temp\ccKuURyc.o:testing.c:(.text+0x172): undefined reference to `complex_add'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\HJPOTT~1\AppData\Local\Temp\ccKuURyc.o: bad reloc address 0x20 in section `.eh_frame'
c:/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
creating assembled outputs and generating output using those:
C:\Users\hjpotter92\Documents\Programming>gcc -c testing.c
C:\Users\hjpotter92\Documents\Programming>gcc -c complex.c
C:\Users\hjpotter92\Documents\Programming>gcc complex.o testing.o
testing.o:testing.c:(.text+0x7e): undefined reference to `complex_mul'
testing.o:testing.c:(.text+0xe8): undefined reference to `complex_sub'
testing.o:testing.c:(.text+0x172): undefined reference to `complex_add'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: testing.o: bad reloc address 0x20 in section `.eh_frame'
c:/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
No difference in outputs is observed on passing -Wall parameter.
Can someone help me regarding the issue? The program seems to be pretty straightforward and there shouldn't be any problems with compiling and executing it.
PS: The program works perfectly on an Arch machine:
archdesktop% gcc complex.c testing.c
archdesktop% ./a.out
0.00 + 153.00i
-9.00 + 9.00i
15.00 + 15.00i
GCC details:
C:\Users\hjpotter92\Documents\Programming>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-mp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)
I used mingw-get to install gcc.
Apparently, the suggestion of using -v flag while compiling seems to compile the program without errors. Once I successfully compiled the program with -v, gcc isn't running into any errors with any of the methods I was trying before.
C:\Users\hjpotter92\Documents\Programming>gcc -v testing.c complex.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/cc1.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/4.8.1/ testing.c -quiet -dumpbase testing.c -mtune=generic -march=pentiumpro -auxbase testing -version -o C:\Users\HJPOTT~1\AppData\Local\Temp\ccDBoHzE.s
GNU C (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../include"
ignoring duplicate directory "/mingw/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include-fixed"
ignoring nonexistent directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include-fixed
End of search list.
GNU C (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 767202a405449496e68a54c4eee91a28
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/as.exe -v -o C:\Users\HJPOTT~1\AppData\Local\Temp\ccc6ue9i.o C:\Users\HJPOTT~1\AppData\Local\Temp\ccDBoHzE.s
GNU assembler version 2.24 (mingw32) using BFD version (GNU Binutils) 2.24
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/cc1.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/4.8.1/ complex.c -quiet -dumpbase complex.c -mtune=generic -march=pentiumpro -auxbase complex -version -o C:\Users\HJPOTT~1\AppData\Local\Temp\ccDBoHzE.s
GNU C (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../include"
ignoring duplicate directory "/mingw/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include-fixed"
ignoring nonexistent directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include-fixed
End of search list.
GNU C (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 767202a405449496e68a54c4eee91a28
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/as.exe -v -o C:\Users\HJPOTT~1\AppData\Local\Temp\ccPq9b2h.o C:\Users\HJPOTT~1\AppData\Local\Temp\ccDBoHzE.s
GNU assembler version 2.24 (mingw32) using BFD version (GNU Binutils) 2.24
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.8.1/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/collect2.exe -Bdynamic c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/4.8.1/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/4.8.1 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../.. C:\Users\HJPOTT~1\AppData\Local\Temp\ccc6ue9i.o C:\Users\HJPOTT~1\AppData\Local\Temp\ccPq9b2h.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/4.8.1/crtend.o
I've been having some trouble with the installed version of libsasl2 (Cyrus SASL).
In particular, it seems that the local headers and sasl_version report version 2.1.26, but no symbol is provided for the global function sasl_client_done.
I'm pretty sure I'm supposed to have that symbol because:
It's there in the provided sasl/sasl.h header
The cyrsus sasl NEWS file lists "Implemented sasl_client_done()/sasl_server_done()" as a 2.1.24 feature
It's there everywhere that provides 2.1.26 outside of Yosemite
For a reproduction:
note that the sample below prints
"impl: 'Cyrus SASL', version: 33619994, major: 2, minor: 1, step: 26"
the sample compiles and executes on a linux install with the same library version after uncommenting the code
the uncommented code produces a link error on yosemite
Undefined symbols for architecture x86_64:
"_sasl_client_done", referenced from:
_main in foo-072675.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
invoking the compiler with:
clang -Wall -Werror -lsasl2 -o foo foo.c -v
with clang -v of:
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name foo.c -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 241.9 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -Wall -Werror -fdebug-compilation-dir /Users/jcarey/work -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fdiagnostics-show-option -vectorize-slp -o /var/folders/wq/jypwqgv976n0db5l5qxw900r0000gq/T/foo-92054e.o -x c foo.c
clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin14.0.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -o foo -lsasl2 /var/folders/wq/jypwqgv976n0db5l5qxw900r0000gq/T/foo-92054e.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_sasl_client_done", referenced from:
_main in foo-92054e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And the code in question:
#include <sasl/sasl.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char ** argv) {
const char *impl;
int version;
uint32_t buf;
uint16_t major;
uint8_t minor;
uint8_t step;
sasl_version(&impl, &version);
buf = version;
major = buf >> 24;
minor = (buf << 8) >> 24;
step = (buf << 24) >> 24;
printf("impl: '%s', version: %d, major: %d, minor: %d, step: %d\n", impl, version, major, minor, step);
/*
{
int (* scd)(void);
scd = &sasl_client_done;
printf("sasl_client_done: %p\n", scd);
}
*/
return 0;
}
I'm thinking that something's screwy with the way cyrus sasl got packaged for Yosemite (using a symbol list from Mavericks perhaps?).
As a matter of interest, I just checked with 10.10.4 and I see the symbol is now there:
$ nm /usr/lib/libsasl2.dylib |grep sasl_client_done
000000000000724a T _sasl_client_done
The sample code now works fine (with the commented section now uncommented). The same Cyrus SASL version is still returned though:
impl: 'Cyrus SASL', version: 33619994, major: 2, minor: 1, step: 26
sasl_client_done: 0x7fff8e3dc24a
I defined some constants in flag.h so link.c and linkedlist.h can use it.
But when I compile with:
clang -Wall main.c link.c linkedlist.c
I get the following
/tmp/linkedlist-o2mcAI.o:(.rodata+0x0): multiple definition of `VARIABLE_NAME'
/tmp/link-oXhyfE.o:(.rodata+0x0): first defined here
for all variables from flag.h used in link.c and linkedlist.c, and at the end:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
A example code of main.c, flag.h, link.h, link.c, linkedlist.h and linkedlist.c:
main.c
#include <stdio.h>
#include "linkedlist.h"
int main(void) {
LinkedList* list = LinkedList_new();
}
flag.h
#ifndef FLAG_H_
#define FLAG_H_
#include <limits.h>
#define FALSE 0
#define TRUE 1
const int OK = 1;
const int ERROR = -1;
const int FLAG = 0;
// other constants
#endif
link.h
#ifndef LINK_H_
#define LINK_H_
typedef struct Link {
int value;
struct Link* next;
} Link;
Link* Link_new(int value);
int useExample(int value);
// other methods
#endif
link.c
#include <stdio.h>
#include <stdlib.h>
#include "link.h"
#include "flag.h"
Link* Link_new(int value)
{
Link* link = malloc(sizeof(Link));
link->value = value;
link->next = NULL;
return link;
}
useExample(int value)
{
if (value == 0) {
return OK; // defined in flag.h
} else {
return FLAG; // defined in flag.h
}
}
// other methods
linkedlist.h
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include "link.h"
typedef struct LinkedList {
Link* first;
unsigned int size;
} LinkedList;
LinkedList* LinkedList_new();
int anotherUseExample(int value);
// other methods
#endif
linkedlist.c
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
#include "flag.h"
LinkedList* LinkedList_new() {
LinkedList* list = malloc(sizeof(LinkedList));
list->first = NULL;
list->size = 0;
return list;
}
anotherUseExample(int value)
{
if (value == 0) {
return FLAG; // defined in flag.h
} else {
return ERROR; // defined in flag.h
}
}
// other methods
So how can I use flag.h in link.c and linkedlist.c without occur multiple definition?
And...
The way I'm coding header files and compiling is correct?
The complete output with -v:
clang version 3.3 (tags/RELEASE_33/rc3)
Target: i386-redhat-linux-gnu
Thread model: posix
"/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/main-JmZTmN.o -x c main.c
clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.3/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.8.1/include
End of search list.
"/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name link.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/link-FtygcZ.o -x c link.c
clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.3/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.8.1/include
End of search list.
"/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name linkedlist.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/linkedlist-n0zF1a.o -x c linkedlist.c
clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.3/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.8.1/include
End of search list.
"/usr/bin/ld" --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crt1.o /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crti.o /usr/lib/gcc/i686-redhat-linux/4.8.1/crtbegin.o -L/usr/lib/gcc/i686-redhat-linux/4.8.1 -L/usr/lib/gcc/i686-redhat-linux/4.8.1/../../.. -L/lib -L/usr/lib /tmp/main-JmZTmN.o /tmp/link-FtygcZ.o /tmp/linkedlist-n0zF1a.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-redhat-linux/4.8.1/crtend.o /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crtn.o
/tmp/linkedlist-n0zF1a.o:(.rodata+0x4): multiple definition of `ERROR_indexOutOfBounds'
/tmp/link-FtygcZ.o:(.rodata+0x4): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0x8): multiple definition of `ERROR_invalidArgument'
/tmp/link-FtygcZ.o:(.rodata+0x8): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0x10): multiple definition of `FLAG_notFound'
/tmp/link-FtygcZ.o:(.rodata+0x10): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0xc): multiple definition of `FLAG_undefined'
/tmp/link-FtygcZ.o:(.rodata+0xc): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0x0): multiple definition of `OK'
/tmp/link-FtygcZ.o:(.rodata+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The #include directive in C simply copies the text from the header file. That means that when you compile both link.c and linkedlist.c, the constant definitions from flag.h gets copied into both, and all these constants are now defined in both link.o and linkedlist.o`. When you link the program you get a name you get a multiple definition error.
You need to split the declaration from the definition, and create a flag.c file where you define const int OK = 1; etc., and at flag.h you'll stay with const int OK; etc. That way, the constants will be compiled into a single object file - flag.o - and you won't get a multiple definition error when you link.
Since you don't need these constants as objects, you should just use a different feature to define them, enumerations.
enum { OK = 1, ERROR = -1, FLAG = 0, ONE = 1, };
These are of type int and will never give rise to multiple symbol errors. Their advantage is that they can remain inside the .h file. Thereby there value is visible to all functions that use them and can better be optimized.
As can be seen in the example above, values don't have to appear in order and the same value may appear several times.
flag.h:
extern const int OK, ERROR, FLAG;
flag.c:
const int OK = 1;
const int ERROR = -1;
const int FLAG = 0;
#Idan Arye and #Jens Gustedt gave two solutions. Both have advantages and disadvantages.
One main advantage with enum is that we need not assign any values to elements and compiler will automatically assign values. There is less chance for duplicate entries and we can add or remove new elements safely.
Disadvantage with enum is that it is int by default. If we need uint8_t in memory constrained systems like embedded programming, we may face problems. But some compilers support -fshort-enums compiler option which allows the compiler to set the size of an enumeration type to the smallest data type that can hold all enumerator values. But we have to set these options specifically.
Also Forward declaration of enumerated type of variable is not possible in C
With const usage in header file and in source file, again we need to copy all consts to both the files.
Also values may get duplicated (Dangerous in case values have to be unique).
Another main disadvantage with const is that they cannot be used for initializing static or global variables. They are not treated as compile time constants instead they are treated as read-only variables.