0

My app compile and worked fine with:

g++ -shared -o myExten.so exm2.o main.o exm1.o -lphpcpp

If I change shared to static, I get an error:

g++ -static -o myExten.so exm2.o main.o exm1.o -lphpcpp

/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In function _start': /build/glibc-Ir_s5K/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference tomain' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libphpcpp.a(value.o): In function Php::Value::Value()': /root/MyTest/full/zend/value.cpp:40: undefined reference to_emalloc' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libphpcpp.a(value.o): In function Php::Value::Value(decltype(nullptr))': /root/MyTest/full/zend/value.cpp:50: undefined reference to_emalloc' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libphpcpp.a(value.o): In function Php::Value::Value(short)': /root/MyTest/full/zend/value.cpp:61: undefined reference to_emalloc' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libphpcpp.a(value.o): In function Php::Value::Value(int)': /root/MyTest/full/zend/value.cpp:72: undefined reference to_emalloc' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libphpcpp.a(value.o): In function Php::Value::Value(long)': /root/MyTest/full/zend/value.cpp:83: undefined reference to_emalloc' /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libphpcpp.a(value.o):/root/MyTest/full/zend/value.cpp:94: more undefined references to `_emalloc' follow ........

How do I compile to a static library?

alk
  • 69,737
  • 10
  • 105
  • 255
nima
  • 177
  • 1
  • 1
  • 6
  • possible duplicate of [How to create a static library with g++?](http://stackoverflow.com/questions/5947067/how-to-create-a-static-library-with-g) – Alex Lop. Sep 05 '15 at 09:50

1 Answers1

4

There are several issues.

static is not an inverse of shared.

-static
On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

-shared
Produce a shared object which can then be linked with other objects to form an executable.

As you can see, -shared determines what kind of output file is produced, while -static determines what kinds of input files are consumed.

If you want to create a shared library that doesn't link to other shared libraries, you need to:

  1. Specify both -shared and -static. If you don't specify shared, the linker will attempt to create an executable.
  2. Enumerate all needed libraries on the command line, not just those you reference directly. For example, if you link to libA and libA calls functions from libB, you must (unlike the case of shared libraries) mention libB on the link line. This is because:

Static libraries are dumb.

They are simple archives of object files. They contain no other libraries or references thereto. While libphpcpp.so knows it's using libphp.so, libphpcpp.a has no idea. It just has an unresolved reference to emalloc. It's your job to supply a library that provides emalloc in order to satisfy the reference.

In addition, if you want to build a static library, you should know that:

Neither the compiler nor the linker produce static libraries.

There is a separate tool called ar for that. And since static libraries are dumb (see above), you should not and cannot pass ar any other libraries to link against. ar doesn't link, it creates a dumb archive to be linked later. When linking your program against your static library, you will have to specify not only your library, but also -lphpcpp, -lphp and any other static library you are referencing directly or indirectly.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243