[英]Shared libraries C++ Makefile
I need to compile fat binary file to be able use it on another linux machine. 我需要编译胖二进制文件才能在另一台Linux机器上使用它。 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. 但是我不明白如何为此配置一个Makefile。 Currently my makefile looks like this: 目前,我的makefile如下所示:
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)
You'll better take advantage of the many built-in rules of GNU make. 您最好利用GNU make的许多内置规则。 Run once make -p
to learn them. 运行一次make -p
来学习它们。 So you should use CXX
instead of CC
and replace CC_FLAGS
with CXXFLAGS
. 因此,您应该使用CXX
而不是CC
并将CC_FLAGS
替换为CXXFLAGS
。
You may want to build a statically linked executable. 您可能需要构建静态链接的可执行文件。 Then you should pass -static
into your linking command, using LINKFLAGS
然后,您应该使用LINKFLAGS
将-static
传递给链接命令
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 eg a BSD one). AFAIK,您的Makefile
不需要任何其他信息(前提是您使用的是GNU make
,而不是BSD)。 Of course you need appropriate TAB characters in your Makefile
(so you need to use some editor able to insert them). 当然,您需要在Makefile
中使用适当的TAB字符(因此您需要使用一些能够插入它们的编辑器)。
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). 您可能只想静态链接-lmicrohttpd
(并动态链接其他库;但是,您可能还想静态链接C ++标准库,该库取决于编译器,并且在编译器更改时可能会更改;还静态链接C ++库留作练习)。 You could do that with removing the LINKFLAGS
line and using 您可以删除LINKFLAGS
行并使用
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). 顺便说一句,需要使用-shared
链接器选项从与位置无关的代码目标文件中构建一个共享库(不使用 )。 See this and that . 看到这个和那个 。
You may want to use make --trace
(or remake -x
, using remake ) to debug your Makefile
您可能要使用make --trace
(或remake -x
,使用remake )来调试您的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. 如果您想了解链接了哪些实际文件,则可以通过在终端上运行make 'LINKFLAGS=-v -Wl,--verbose'
向LINKFLAGS
添加-v -Wl,--verbose
。
You might want to make clean
before anything else. 您可能需要先make clean
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.