linux, build shared library with `-pie` instead of `-shared` so it can also be execute
As this link shows https://unix.stackexchange.com/questions/223385/why-and-how-are-some-shared-libraries-runnable-as-though-they-are-executables
test.c
#include <stdio.h>
void sayHello (char *tag) {
printf("%s: Hello!\n", tag);
}
int main (int argc, char *argv[]) {
sayHello(argv[0]);
return 0;
}
compile with gcc -fPIC -pie -o libtest.so test.c -Wl,-E
can make it runnable, and also linkable as shared library.
If use CMakeLists.txt, add -pie
as CMAKE_SHARED_LINKER_FLAGS
and add_library(test SHARED test.c)
, it will be link as
/usr/bin/c++ -fPIC -g -pie -Wl,-E -shared -o libTpDLStream.so ...
a -shared
after -pie
make above solution broken.
how to let CMake build shared library with -pie
instead of -shared
.
Edited by joshuafc