Shared libraries C++ Makefile

2019-07-12 18:19发布

I need to compile fat binary file to be able use it on another linux machine. But there are some libraries missing so as I understand I should compile it with some -shared options. But I don't understand how to configure a Makefile for that. Currently my makefile looks like this:

CC = g++
CC_FLAGS = -std=c++11 -O2 -static -Wall

EXEC = cpp_server
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
LIBS = -lpthread -lmicrohttpd -lz

$(EXEC): $(OBJECTS)
    $(CC) $(OBJECTS) -o $(EXEC) $(LIBS)

%.o: %.cpp
    $(CC) -c $(CC_FLAGS) $< -o $@

clean:
    rm -f $(EXEC) $(OBJECTS)

1条回答
Juvenile、少年°
2楼-- · 2019-07-12 18:58

You'll better take advantage of the many built-in rules of GNU make. Run once make -p to learn them. So you should use CXX instead of CC and replace CC_FLAGS with CXXFLAGS.

You may want to build a statically linked executable. Then you should pass -static into your linking command, using LINKFLAGS

So try with

## untested Makefile for GNU make
# variables known to make
CXX= g++
CXXFLAGS= -std=c++11 -O2 -Wall -Wextra
LINKFLAGS= -static
LIBS= -lpthread -lmicrohttpd -lz
# this project needs:
MYEXEC= cpp_server
MYSOURCES= $(wildcard *.cpp)
MYOBJECTS= $(SOURCES:.cpp=.o)
.PHONY: all clean

all: $(MYEXEC)
$(MYEXEC): $(MYOBJECTS)
   $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@
clean:
   rm -f $(MYEXEC) $(MYOBJECTS)

AFAIK you don't need anything more in your Makefile (provided you use a GNU make, not e.g. a BSD one). Of course you need appropriate TAB characters in your Makefile (so you need to use some editor able to insert them).

You could want to statically link only -lmicrohttpd (and dynamically link the other libraries; however, you might want to also statically link the C++ standard library, which depends upon the compiler and could change when the compiler changes; linking also the C++ library statically is left as an exercise). You could do that with removing the LINKFLAGS line and using

LIBS= -Bstatic -lmicrohttpd -Bdynamic -lz -lpthread

BTW the -shared linker option is need to build from position-independent code object files a shared library (not to use one). See this and that.

You may want to use make --trace (or remake -x, using remake) to debug your Makefile

If you want to understand what actual files are linked, add -v -Wl,--verbose to LINKFLAGS perhaps by running make 'LINKFLAGS=-v -Wl,--verbose' on your terminal.

You might want to make clean before anything else.

查看更多
登录 后发表回答