Search code examples
linuxlinkerglibc

Build C project on CentOS 7, the linker can't use -ldl , -lc


the issue

I started a C project on my local Ubuntu machine. After I completed the first version(the building is ok), I decided to upload the code to a server which runs a CentOS 7 to implement more features.
The code and the makefiles are the same on both sides.
But the code building on the remote shows the error as follows:

/usr/bin/ld: cannot find -ldl /usr/bin/ld: cannot find -lc collect2: error: ld returned 1 exit status

building tools and environment

  • OS: centos7
  • toolchain : gcc

What I tried to fix this

  • yum install glibc-devel

But it did not work. How should I fix this?

Edit , I got a workaround

I found the makefile I wrote a week ago is a little bit strange on LDFLAGS,

LDFLAGS += -L$(LIBPATH) LDFLAGS += -static -lxxx -lyyy -ldl xxx and yyy is the static library name I need to link.

I modified the makefile to LDFLAGS += -L$(LIBPATH) LDFLAGS += -l:libxxx.a -l:libyyy.a -ldl Now it works fine.


Solution

  • -static requires the glibc-static package, not just glibc-devel. But static linking has many limitations (particularly in combination with dlopen), and you should avoid it if at all possible. (It is explicitly unsupported on Red Hat Enterprise Linux.)