Skip to content

Instantly share code, notes, and snippets.

@auzhva

auzhva/Makefile Secret

Created September 22, 2021 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auzhva/6e3660c2433f7ead765d4d09e645013b to your computer and use it in GitHub Desktop.
Save auzhva/6e3660c2433f7ead765d4d09e645013b to your computer and use it in GitHub Desktop.
#include <dlfcn.h>
#include <stdio.h>
int main(const int argc, const char **argv) {
void *h = dlopen(argv[1], RTLD_LAZY);
if (NULL == h) {
printf("error = %s\n", dlerror());
return 1;
}
printf("dlopen is ok\n");
dlclose(h);
}
#include <pthread.h>
// cgo def
extern int get_version();
char get(int idx) {
// C-side __thread var
static __thread char buffer[10*1024*1024];
// Call to go
get_version();
// meaningless, just a sample
return buffer[idx];
}
package main
import "C"
//export get_version
func get_version() C.int {
return 2
}
func main() {
}
default: run
.PHONY: app
app:
gcc app.c -o app -ldl
# This passes as both libGoShared.so and libClibShared.so are dynamic
# So only libGoShared has DF_STATIC_TLS flag and it's only 16 bytes
shared: app
go build -ldflags="-s -w" -o libGoShared.so -buildmode=c-shared ./lib.go
gcc -shared -lpthread -fPIC lib.c -o libClibShared.so -lGoShared -L.
LD_LIBRARY_PATH=. ./app ./libClibShared.so
# This fails as libClibStatic.so contains both C and Go pars
# and inherits DF_STATIC_TLS flag from go and fails on loading
static: app
go build -ldflags="-s -w" -o libGoStatic.a -buildmode=c-archive ./lib.go
gcc -shared -lpthread -fPIC lib.c -o libClibStatic.so -lGoStatic -L.
LD_LIBRARY_PATH=. ./app ./libClibStatic.so
clean:
rm -f *.so
rm -f *.a
rm -f app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment