GCC linking libc static and some other library dynamically, revisited? - c

The following questions are relevant but do not answer my question:
Linking partially static and partially dynamic in GCC
Linking a dynamic library to a static library that links to other static libraries
GCC: static linking only some libraries
Static link of shared library function in gcc
I asked a very similar question earlier, but since the previous question started by me got somewhat cluttered in the comment section and not fully answered (but I flagged it as answered since it was a good effort and did at least partially answer it) I will ask a new question. The question is specifically how to link libc as static, while linking some other library (e.g. libm) dynamically. This was suggested that cannot be done in the first question, is that true? If so it would be very interesting to know why not.
Is it even possible to do this? Someone made a comment (which was removed for some reason, maybe it was incorrect?) that it is possible, but there must then also exist a dynamically linked version of libc, since it will be required by the dynamic library (e.g. dynamic libm will require dynamic libc (?)).
This is fine for me, but it is not obvious to me how to tell GCC to do this, i.e. link in libc as both static and dynamic. How do I do this (I made a couple attempts, some are shown later in the question)? Or is there some other way to do what I want?
We first see that by simply running gcc test.c -lm, everything is linked in dynamically, as follows:
$ gcc test.c -lm
$ ldd a.out
linux-vdso.so.1 (0x00007fffb37d1000)
libm.so.6 => /lib64/libm.so.6 (0x00007f3b0eeb6000)
libc.so.6 => /lib64/libc.so.6 (0x00007f3b0eb10000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3b0f1b0000)
To link only libm as static, while allowing libc to remain dynamic, we can do (as Z boson pointed out in one of the aforementioned questions):
$ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libm.a
$ ldd a.out
linux-vdso.so.1 (0x00007fff747ff000)
libc.so.6 => /lib64/libc.so.6 (0x00007f09aaa0c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f09aadb2000)
However, attempting the same procedure to link libc static and libm dynamic, does not seem to work:
$ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a -lm
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
What does this error message mean?
Some other attempts (most were also included in my first question):
$ gcc test.c /usr/lib64/libc.a
linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
urned 1 exit status
$ gcc test.c -Wl,-Bdynamic -lm -Wl,-Bstatic -lc
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
$ gcc -Wl,-Bdynamic -lm -Wl,-Bstatic -lc test.c
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
$ gcc -Wl,-Bstatic -lc -Wl,-Bdynamic -lm test.c
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
$ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.so -lm
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
$ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.so /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a -lm
Note that the last one compiled/linked successfully. However libc has not been linked in statically, only dynamically, so it is another failed attempt.
The test program is simply the following:
$ cat test.c
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
int i;
int result;
for(i = 0; i < 65535; i++) {
result = sin(i);
}
return 0;
}
Edit:
I've also tried statifier and ermine, as suggested in this question:
Static link of shared library function in gcc
Neither works.

Basically, your first approach is the correct way to do this:
gcc test.c libc.a -lm
After gcc adds the implicit libraries it'll look (conceptually) like this:
gcc crt1.o test.c libc.a -lm -lc -lgcc -lc
So that means that any libc functions called by either crt1.o or test.c will be pulled in from libc.a and linked statically, whereas any functions called solely from libm or libgcc will be linked dynamically (but it will reuse the static functions if libm calls something already pulled in).
The linker always starts at the left-most file/library, and works rightwards; it never goes back. .c and .o files are linked in unconditionally, but .a files and -l options are only used to find functions that are already referenced but not yet defined. Therefore, a library at the left is pointless (and -lc must appear twice because -lc depends on -lgcc, and -lgcc depends on -lc). Link order is important!
Unfortunately, you appear to have been foiled by what might be a bug in strcmp (or rather in the libc that contains strcmp): the STT_GNU_IFUNC thing is a clever feature that allows multiple versions of a function to be included, and the most optimal one to be selected at runtime, based on what hardware is available. I'm not sure, but it looks like this feature is only available in a PIE (Position Independent Executable) or shared library build.
Why that would be in a static libc.a is a mystery to me, but there's an easy workaround: implement your own strcmp (a basic, slow implementation is only a few lines of C), and link it in before libc.a.
gcc test.c mystrcmp.c libc.a -lm
Alternatively, you can extract the functions from libc.a that you really want, and link only those in statically:
ar x libc.a
gcc test.c somefile.o -lm
ar is to .a files, as tar is to .tar files, although the command usage varies a bit, so this example extracts the .o files from the .a file, and then links them explicitly.

Based on the answer by ams I did the follow
mystrcmp.c
int strcmp(const char *s1, const char *s2) {
}
Compile
gcc -c test.c
gcc -c mystrcmp.c
Setup files
ln -s `gcc -print-file-name=crt1.o`
ln -s `gcc -print-file-name=crti.o`
ln -s `gcc -print-file-name=crtn.o`
ln -s `gcc -print-file-name=libgcc_eh.a`
ln -s `gcc -print-file-name=libc.a`
ln -s `gcc -print-file-name=libm.so`
Link
ld -m elf_x86_64 -o math crt1.o crti.o test.o mystrcmp.o libc.a libgcc_eh.a libc.a libm.so -dynamic-linker /lib64/ld-linux-x86-64.so.2 crtn.o
This links and runs correctly. However, ldd shows
linux-vdso.so.1 => (0x00007fff51911000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8182470000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81820a9000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8182793000)
It appears that dynamic libm requires dynamic libc. Actually, that's easy to show
ldd libm.so reports
linux-vdso.so.1 => (0x00007fff20dfe000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcaf74fe000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcaf7bed000)
So it's impossible to link to libm.so without linking libc.so as well unless you manage to compile libm without the dependency on libc.

just use
gcc sample_uart.c -static -static-libgcc -static-libstdc++
# lddtree a.out
a.out => ./a.out (interpreter => none)
#################### test ############################
ar x /usr/lib/arm-linux-gnueabihf/libc.a
-rw-r--r-- 1 root root 792 Oct 28 00:38 wmemset.o
-rw-r--r-- 1 root root 2456 Oct 28 00:38 wmemstream.o
-rw-r--r-- 1 root root 1616 Oct 28 00:38 wordcopy.o
-rw-r--r-- 1 root root 18544 Oct 28 00:38 wordexp.o
-rw-r--r-- 1 root root 1384 Oct 28 00:38 wprintf_chk.o
-rw-r--r-- 1 root root 1088 Oct 28 00:38 wprintf.o
-rw-r--r-- 1 root root 884 Oct 28 00:38 write_nocancel.o
-rw-r--r-- 1 root root 1368 Oct 28 00:38 write.o
-rw-r--r-- 1 root root 1340 Oct 28 00:38 writev.o
-rw-r--r-- 1 root root 1084 Oct 28 00:38 wscanf.o
-rw-r--r-- 1 root root 3924 Oct 28 00:38 wstrops.o
-rw-r--r-- 1 root root 2112 Oct 28 00:38 xcrypt.o
-rw-r--r-- 1 root root 1504 Oct 28 00:38 xdr_array.o
-rw-r--r-- 1 root root 836 Oct 28 00:38 xdr_float.o
-rw-r--r-- 1 root root 2344 Oct 28 00:38 xdr_intXX_t.o
-rw-r--r-- 1 root root 1740 Oct 28 00:38 xdr_mem.o
-rw-r--r-- 1 root root 4696 Oct 28 00:38 xdr.o
-rw-r--r-- 1 root root 3880 Oct 28 00:38 xdr_rec.o
-rw-r--r-- 1 root root 1640 Oct 28 00:38 xdr_ref.o
-rw-r--r-- 1 root root 1556 Oct 28 00:38 xdr_sizeof.o
-rw-r--r-- 1 root root 2464 Oct 28 00:38 xdr_stdio.o
-rw-r--r-- 1 root root 1688 Oct 28 00:38 xlocale.o
-rw-r--r-- 1 root root 936 Oct 28 00:38 xmknodat.o
-rw-r--r-- 1 root root 944 Oct 28 00:38 xmknod.o
-rw-r--r-- 1 root root 1052 Oct 28 00:38 xpg_basename.o
-rw-r--r-- 1 root root 1640 Oct 28 00:38 xpg-strerror.o
-rw-r--r-- 1 root root 900 Oct 28 00:38 xstat64.o
-rw-r--r-- 1 root root 1184 Oct 28 00:38 xstatconv.o
-rw-r--r-- 1 root root 1196 Oct 28 00:38 xstat.o
root#hi3798mv100:~/sample/uart#
root#hi3798mv100:~/sample/uart#
root#hi3798mv100:~/sample/uart# gcc sample_uart.c -lm
root#hi3798mv100:~/sample/uart# gcc sample_uart.c *.o -lm
/usr/bin/ld: dso_handle.o:(.data.rel.ro.local+0x0): multiple definition of `__dso_handle'; /usr/lib/gcc/arm-linux-gnueabihf/9/crtbeginS.o:(.data.rel.local+0x0): first defined here
/usr/bin/ld: rcmd.o: in function `__validuser2_sa':
(.text+0x418): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status

Related

Function declared in openssl/sha.h but not found in shared library

I've tried to link my C program against libssl.so, but the linker fails to find a function in that library that does exist in the header file.
Makefile target code:
$(CC) -o $# $^ $(CFLAGS) -lssl -lmagic
Output:
...
/usr/bin/ld: obj/signature.o: undefined reference to symbol 'SHA256_Init##OPENSSL_1_1_0'
//usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
...
The function is SHA256_Init() and it is present in openssl/sha.h that it included in the source file.
I'm on Debian 9. I have the following packages installed: libssl1.0.0, libssl1.0.2, libssl1.1, libssl-dev (1.1.0).
$ la /usr/lib/x86_64-linux-gnu/libssl*
-rw-r--r-- root root 357024 /usr/lib/x86_64-linux-gnu/libssl3.so
-rw-r--r-- root root 738444 /usr/lib/x86_64-linux-gnu/libssl.a
lrwxrwxrwx root root 13 /usr/lib/x86_64-linux-gnu/libssl.so -> libssl.so.1.1
-rw-r--r-- root root 395176 /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0
-rw-r--r-- root root 431232 /usr/lib/x86_64-linux-gnu/libssl.so.1.0.2
-rw-r--r-- root root 442920 /usr/lib/x86_64-linux-gnu/libssl.so.1.1
What am I doing wrong and how to solve the problem?
OpenSSL is split into 2 libraries, libssl and libcrypto, these functions live in libcrypto, so you don't need to link to libssl. Use
-lcrypto
You should also read this documentation for more info, e.g. how to properly initialize libcrypto

"Undefined symbols for architecture x86_64:" for library using cgo on macOS Sierra

I am trying to make use of a library, https://github.com/go-steem/rpc, that makes use of some C code, which references a library.
The C library can be found here, https://github.com/bitcoin-core/secp256k1
I followed the steps to get that installed
$ ./autogen.sh
$ ./configure
$ make
$ ./tests
$ sudo make install # optional
And have this output;
$ sudo make install
Password:
CC src/libsecp256k1_la-secp256k1.lo
CCLD libsecp256k1.la
CC src/tests-tests.o
CCLD tests
CC src/exhaustive_tests-tests_exhaustive.o
CCLD exhaustive_tests
build-aux/install-sh -c -d '/usr/local/lib'
/bin/sh ./libtool --mode=install /usr/bin/install -c libsecp256k1.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libsecp256k1.0.dylib /usr/local/lib/libsecp256k1.0.dylib
libtool: install: (cd /usr/local/lib && { ln -s -f libsecp256k1.0.dylib libsecp256k1.dylib || { rm -f libsecp256k1.dylib && ln -s libsecp256k1.0.dylib libsecp256k1.dylib; }; })
libtool: install: /usr/bin/install -c .libs/libsecp256k1.lai /usr/local/lib/libsecp256k1.la
libtool: install: /usr/bin/install -c .libs/libsecp256k1.a /usr/local/lib/libsecp256k1.a
libtool: install: chmod 644 /usr/local/lib/libsecp256k1.a
libtool: install: /usr/bin/ranlib /usr/local/lib/libsecp256k1.a
build-aux/install-sh -c -d '/usr/local/include'
/usr/bin/install -c -m 644 include/secp256k1.h '/usr/local/include'
build-aux/install-sh -c -d '/usr/local/lib/pkgconfig'
/usr/bin/install -c -m 644 libsecp256k1.pc '/usr/local/lib/pkgconfig'
I try to run the upvote example from that Go library, go-steem/rpc/examples/upvote/ and get the following output;
$ go run main.go
# github.com/go-steem/rpc/transactions
../../transactions/signing.c:5:10: fatal error: 'secp256k1.h' file not found
Already it feels as though the wheels are falling off...
Please bear with me as I do not develop in C, so I get a bit hack-y.
After much reading, and googling I decide to copy the files from the 'include' directory where I compiled libsecp256k1 into the same directory as the error is originating from.
You can see the files are not there;
$ ls -la ../../transactions/
total 48
drwxr-xr-x 8 shaunmorrow staff 272 May 8 18:09 .
drwxr-xr-x 15 shaunmorrow staff 510 May 8 18:09 ..
-rw-r--r-- 1 shaunmorrow staff 256 Apr 27 17:53 chains.go
-rw-r--r-- 1 shaunmorrow staff 3731 May 8 18:09 signed_transaction.go
-rw-r--r-- 1 shaunmorrow staff 1849 May 8 18:09 signed_transaction_test.go
-rw-r--r-- 1 shaunmorrow staff 3075 Apr 27 17:53 signing.c
-rw-r--r-- 1 shaunmorrow staff 408 Apr 27 17:53 signing.h
-rw-r--r-- 1 shaunmorrow staff 1049 May 8 18:09 transactions.go
and after the copy;
$ ls -la ../../transactions/
total 128
drwxr-xr-x 11 shaunmorrow staff 374 Jul 18 19:08 .
drwxr-xr-x 15 shaunmorrow staff 510 May 8 18:09 ..
-rw-r--r-- 1 shaunmorrow staff 256 Apr 27 17:53 chains.go
-rw-r--r-- 1 shaunmorrow staff 27071 Jul 18 19:08 secp256k1.h
-rw-r--r-- 1 shaunmorrow staff 1014 Jul 18 19:08 secp256k1_ecdh.h
-rw-r--r-- 1 shaunmorrow staff 4700 Jul 18 19:08 secp256k1_recovery.h
-rw-r--r-- 1 shaunmorrow staff 3731 Jul 18 19:05 signed_transaction.go
-rw-r--r-- 1 shaunmorrow staff 1849 May 8 18:09 signed_transaction_test.go
-rw-r--r-- 1 shaunmorrow staff 3075 Apr 27 17:53 signing.c
-rw-r--r-- 1 shaunmorrow staff 408 Apr 27 17:53 signing.h
-rw-r--r-- 1 shaunmorrow staff 1049 May 8 18:09 transactions.go
Now I get a new error;
$ go run main.go
# github.com/go-steem/rpc/transactions
ld: library not found for -lsecp256k1
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This has me reading and googling some more,
Finally I get even more hack-y and change transactions.go;
// #cgo LDFLAGS: -lsecp256k1
// #include <stdlib.h>
// #include "signing.h"
import "C"
becomes
// #cgo LDFLAGS: -L/usr/local/lib
// #include <stdlib.h>
// #include "signing.h"
import "C"
which fails, output on that later
I also try;
// #cgo LDFLAGS: -L/usr/local/lib -I/usr/local/include
// #include <stdlib.h>
// #include "signing.h"
import "C"
and copy the .h files into the /usr/local/include directory.
None of this works and now I am stuck with an error like this
$ go run main.go
# github.com/go-steem/rpc/transactions
ld: library not found for -lsecp256k1
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ShaunsSePc-2:upvote shaunmorrow$ go run main.go
# github.com/go-steem/rpc/transactions
Undefined symbols for architecture x86_64:
"_secp256k1_context_create", referenced from:
_sign_transaction in signing.o
_verify_recoverable_signature in signing.o
"_secp256k1_context_destroy", referenced from:
_sign_transaction in signing.o
_verify_recoverable_signature in signing.o
"_secp256k1_ec_pubkey_serialize", referenced from:
_verify_recoverable_signature in signing.o
"_secp256k1_ecdsa_recover", referenced from:
_verify_recoverable_signature in signing.o
"_secp256k1_ecdsa_recoverable_signature_convert", referenced from:
_verify_recoverable_signature in signing.o
"_secp256k1_ecdsa_recoverable_signature_parse_compact", referenced from:
_verify_recoverable_signature in signing.o
"_secp256k1_ecdsa_recoverable_signature_serialize_compact", referenced from:
_sign_transaction in signing.o
"_secp256k1_ecdsa_sign_recoverable", referenced from:
_sign_transaction in signing.o
"_secp256k1_ecdsa_verify", referenced from:
_verify_recoverable_signature in signing.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
At this point I really have no idea how to continue.
As you can see I am not experienced in C at all and have no idea how to test if the library libsecp256k1 is even installed properly!
This is where I ended up, but it's highly likely I took a wrong turn early in my journey, I would appreciate any help given as I have been struggling with this for a few nights already now :(
Not sure whats needed so here some env variables
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/shaunmorrow/Work/go/"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/86/hlqptn5101z5bcydjz05qy8m0000gn/T/go-build689438019=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
Version is go version go1.8.3 darwin/amd64 with no old versions lying around.
Thanks!
Your problem is twofold:
Improperly configured libsecp256k1 package
Your C compiler not searching /usr/local directories for installed headers/libs
Fixing the improperly libsecp256k1 configured package
Your "undefined symbols" issue when it comes to linking C libraries sometimes points to an improperly configured package (in the sense of an Autotools package or a CMake package, not a Go package). Running ./configure --help, I see there is an option named --enable-module-recovery. Judging by names like _secp256k1_ecdsa_sign_recoverable and _secp256k1_ecdsa_recover, you need to add that option when configuring, meaning instead of executing the simpler ./configure, you should execute this:
./configure --enable-module-recovery
Is the C compiler broken?
Since the secp256k1.h header file isn't found in /usr/local/include, despite the fact that the header file most definitely exists after sudo make install is finished, it means your compiler doesn't search /usr/local.
Barring any fixes in the linked question, you can work around the issue by altering the source of the Go package as needed to add/modify the CFLAGS and LDFLAGS used when dealing with import "C" statements like this:
// #cgo CFLAGS: -I/usr/local/include
// #cgo LDFLAGS: -L/usr/local/lib -lsecp256k1
// #include <secp256k1.h>
import "C"
If you have pkg-config installed, you can use that instead of setting CFLAGS and LDFLAGS manually:
Export the PKG_CONFIG_PATH environment variable with a custom set of paths. Because no prefix (i.e. directory serving as the install root) was specified, /usr/local is assumed, meaning /usr/local/include will contain headers and /usr/local/lib will contain libraries. This means you need to export PKG_CONFIG_PATH on your command line as in export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig. The default setting of PKG_CONFIG_PATH when unset includes /usr/lib/pkgconfig and /usr/share/pkgconfig in that order on Linux and are specified as a fallback for other packages that might be used, though it may not be necessary in this case. The default set of paths may differ on OS X, so consult your pkg-config man page for reference.
Use // #cgo pkg-config: libsecp256k1, and CFLAGS and LDFLAGS will be set as necessary without you needing to do anything to them. Since not all systems have pkg-config installed, relying on this is perhaps a bad idea if you want a package to remain portable. Then again, I think it'd be preferable to the mess you dealt with since pkg-config simply wouldn't be found.
Change to the upvote directory and type make to build a working upvote binary.
Additional customization
Custom prefix
If you use something other than /usr/local as your prefix (e.g. ./configure --enable-module-recovery --prefix=/opt/libsecp256k1), then you'll need to adjust some things accordingly:
// #cgo CFLAGS: -I/opt/libsecp256k1/include
// #cgo LDFLAGS: -L/opt/libsecp256k1/lib -lsecp256k1
// #include "secp256k1.h"
import "C"
// or just use pkg-config and export the PKG_CONFIG_PATH environment
// variable containing the following paths:
// /opt/libsecp256k1/lib/pkgconfig
// /opt/libsecp256k1/share/pkgconfig
// /usr/lib/pkgconfig
// /usr/share/pkgconfig
You'll also need to modify the provided Makefile in the upvote directory to set the runtime path of the binary that gets built, else libsecp256k1.0.dylib will not be found:
# If you copy and paste this, replace the spaces in front of `go build`
# with a single horizontal tab character, else `make` will fail.
#
# Note that the "ldflags" specified are for the Go linker (go tool link),
# not the system's linker (ld).
build:
go build -ldflags="-r /opt/libsecp256k1/lib"
For more information about working with cgo, check out the following resources:
"C? Go? Cgo!" introduction to cgo on The Go Blog
Cgo documentation

Why does gcc prefer dynamic library to static one during linking process?

I compile and link a simple program:
# gcc a.c -pthread
# ldd a.out
linux-vdso.so.1 (0x00007ffc4ddf2000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007fbb75e12000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fbb75a6e000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbb76030000)
The program links the shared library. But from gcc manual:
-llibrary
-l library
......
The linker searches a standard list of directories for the library,
which is actually a file named liblibrary.a. The linker then uses this
file as if it had been specified precisely by name.
......
It seems the program should link the static library. I find there are both static and dynamic libraries:
# ls -alt /usr/lib/libpthread*
-rw-r--r-- 1 root root 392576 Mar 6 22:06 /usr/lib/libpthread.a
lrwxrwxrwx 1 root root 18 Mar 6 22:06 /usr/lib/libpthread.so.0 -> libpthread-2.25.so
-rw-r--r-- 1 root root 1748 Mar 6 22:06 /usr/lib/libpthread_nonshared.a
-rwxr-xr-x 1 root root 146824 Mar 6 22:06 /usr/lib/libpthread-2.25.so
-rw-r--r-- 1 root root 222 Mar 6 22:06 /usr/lib/libpthread.so
Why does gcc choose the dynamic one? BTW, is there any difference when using -lpthread and pthread as the link option during compiling program?

DL_LIBRARY_PATH doesn't work

I madeC programs named drive.c and mylib.c.
drive.c is main module mylib.c is sub modulle that I want work as shared library .
I can compile them with this step on MINGW
gcc –fPIC –g –c –Wall mylib.c
gcc -shared -Wl,-soname,libmylib.so.1 -o /c/opt/lib/libmylib.so.1.0.1 mylib.o -lc
gcc -g -Wall -Wextra -pedantic -I./ -L/c/opt/lib -o drive.exe drive.c –l:libmylib.so.1
Finally I did drive.exe
Then Windows Dialog Message was shown program can start because libmylib.so.1.0.1 is missing.
LD_LIBRARY_PATH is set.
$ set|grep LD
LD_LIBRARY_PATH=:/c/opt/lib
$
And there is libmylib.so.1.0.1
$ ls -la /c/opt/lib
total 98
drwxr-xr-x 2 JAC484 Administrators 4096 Mar 18 14:44 .
drwxr-xr-x 7 JAC484 Administrators 4096 Mar 14 15:47 ..
-rwxr-xr-x 1 JAC484 Administrators 45356 Mar 18 14:23 libmylib.so.1
-rwxr-xr-x 1 JAC484 Administrators 45356 Mar 18 14:23 libmylib.so.1.0.1
If I copied libmylib.so.1.0.1 in same directory of drive.exe,drive.exe can run.
How Can I tell the system where libmylib.so.1.0.1 is?
Windows doesn't use LD_LIBRARY_PATH. Your shared library DLL will need to be in the same directory as drive.exe or in a directory in the PATH.
The full details of Windows' DLL search is documented here:
MSDN: Dynamic-Link Library Search Order

Building patched 64 bit OpenSSL on OSX and Ubuntu

I'm trying to setup a patched version of OpenSSL to use DTLS and I'm having a lot of trouble. I'm assuming it is due to my lack of understanding of gcc and linking c libraries. In particular, I keep on seeing people say to link to the lib/ subfolder, but I cannot find one for OpenSSL. I also a question on building 32 bit OpenSSL, but I'm trying to do 64 bit.
OSX
Getting the source and patch:
wget ftp://ftp.openssl.org/source/openssl-1.0.1c.tar.gz # get latest stable OpenSSL
mv ~/Downloads/openssl-1.0.1c.tar.gz /usr/local/openssl-1.0.1c.tar.gz
cd /usr/local/openssl-1.0.1c.tar.gz
wget http://sctp.fh-muenster.de/dtls/dtls-bugs-1.0.1.patch # get the patch file
Building (64 bit, OpenSSL defaults to 32 bit):
export CFLAGS="-arch x86_64"
export LDFLAGS="-arch x86_64"
./Configure darwin64-x86_64-cc # 64 bit config command
make # .a files should be built, great
Great, I have some libraries in the OpenSSL root directory:
/usr/local/openssl-1.0.1c$ ll lib*
-rw-r--r-- 1 nflacco staff 3286136 Jan 4 12:43 libcrypto.a
-rw-r--r-- 1 nflacco staff 260 Jan 4 12:43 libcrypto.pc
-rw-r--r-- 1 nflacco staff 570200 Jan 4 12:43 libssl.a
-rw-r--r-- 1 nflacco staff 275 Jan 4 12:43 libssl.pc
Now I'll try to compile a simple piece of code that uses the patched OpenSSL:
~$ gcc -L /usr/local/openssl-1.0.1c -lssl -lcrypto -I /usr/local/opt/openssl/include -o server server.c
ld: warning: _OPENSSL_ia32cap_P has different visibility (hidden) in /usr/local/openssl-1.0.1c/libcrypto.a(x86_64cpuid.o) and (default) in /usr/local/openssl-1.0.1c/libcrypto.a(cryptlib.o)
Undefined symbols for architecture x86_64:
"_BIO_dgram_get_peer", referenced from:
_generate_cookie_callback in ccfldIrE.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [server] Error 1
Ubuntu
Building:
./config
make
Checking for libraries (Disregard date, Ubuntu thinks it's Christmas):
/usr/local/openssl-1.0.1c$ ll lib*
-rw-r--r-- 1 root root 3170340 Dec 25 17:45 libcrypto.a
-rw-r--r-- 1 root root 264 Dec 25 17:46 libcrypto.pc
-rw-r--r-- 1 root root 534092 Dec 25 17:45 libssl.a
-rw-r--r-- 1 root root 279 Dec 25 17:46 libssl.pc
And, trying to compile:
gcc -L /usr/local/openssl-1.0.1c -lssl -lcrypto -I /usr/local/opt/openssl/include -o server server.c
/tmp/cc0DgDl1.o: In function `generate_cookie_callback':
server.c:(.text+0x8b): undefined reference to `RAND_bytes'
server.c:(.text+0xba): undefined reference to `SSL_get_rbio'
server.c:(.text+0xdc): undefined reference to `BIO_ctrl'
server.c:(.text+0x112): undefined reference to `CRYPTO_malloc'
/tmp/cc0DgDl1.o: In function `main':
server.c:(.text+0x163): undefined reference to `SSL_library_init'
server.c:(.text+0x168): undefined reference to `SSL_load_error_strings'
server.c:(.text+0x16d): undefined reference to `SSL_library_init'
/tmp/cc0DgDl1.o: In function `configure_server_ssl':
server.c:(.text+0x2f5): undefined reference to `SSL_CTX_set_cipher_list'
server.c:(.text+0x318): undefined reference to `SSL_CTX_ctrl'
server.c:(.text+0x333): undefined reference to `SSL_CTX_use_certificate_file'
server.c:(.text+0x35e): undefined reference to `SSL_CTX_use_PrivateKey_file'
server.c:(.text+0x379): undefined reference to `SSL_CTX_check_private_key'
server.c:(.text+0x3a4): undefined reference to `SSL_CTX_set_verify'
server.c:(.text+0x3c7): undefined reference to `SSL_CTX_ctrl'
server.c:(.text+0x3da): undefined reference to `SSL_CTX_set_cookie_generate_cb'
server.c:(.text+0x3ed): undefined reference to `SSL_CTX_set_cookie_verify_cb'
/tmp/cc0DgDl1.o: In function `start_server':
server.c:(.text+0x40b): undefined reference to `DTLSv1_server_method'
server.c:(.text+0x413): undefined reference to `SSL_CTX_new'
collect2: ld returned 1 exit status
UPDATE:
On Ubuntu I got it to compile by moving the libraries to the end of the compile command AND adding the flag -ldl to compile with no warnings:
gcc -L /usr/local/openssl-1.0.1c -I /usr/local/opt/openssl/include -o server server.c -lssl -lcrypto -ldl
On OSX, this command gives me the same error as earlier with not finding _BIO_dgram_get_peer.
You have to place the libraries last on the command line:
gcc -L /usr/local/openssl-1.0.1c -I /usr/local/opt/openssl/include -o server server.c -lssl -lcrypto
# ^^^^^^^^^^^^^^
There are any number of duplicates of this problem here on this website explaining the whys and hows. The documentation for ld explains it well and is the ultimate reference.

Resources