共享库C ++ Makefile [英] Shared libraries C++ Makefile

查看:75
本文介绍了共享库C ++ Makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编译胖二进制文件才能在另一台Linux机器上使用它.但是有一些库丢失了,据我了解,我应该使用一些共享选项对其进行编译.但是我不明白如何为此配置一个Makefile.目前,我的makefile如下所示:

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)

推荐答案

您最好利用GNU make的许多内置规则. make -p运行一次以了解它们.因此,您应该使用CXX而不是CC并将CC_FLAGS替换为CXXFLAGS.

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.

您可能想构建一个静态链接的可执行文件.然后,应使用LINKFLAGS

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

所以尝试

## 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,您在Makefile中不需要任何其他信息(前提是您使用的是GNU make,而不是BSD).当然,您在Makefile中需要适当的 TAB 字符(因此您需要使用一些能够插入它们的编辑器).

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).

您可能只想静态链接-lmicrohttpd(并动态链接其他库;但是,您可能还想静态链接C ++标准库,该库取决于编译器,并且在编译器更改时可能会更改;还链接静态的C ++库留作练习).您可以删除LINKFLAGS行并使用

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

顺便说一句,需要从与位置无关的代码对象提供了一个共享库(请勿使用使用).请参见 a>.

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.

您可能想使用make --trace(或remake -x,使用 remake )来调试您的Makefile

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

如果您想了解链接了哪些实际文件,则可以通过在终端上运行make 'LINKFLAGS=-v -Wl,--verbose'-v -Wl,--verbose添加到LINKFLAGS.

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 clean.

这篇关于共享库C ++ Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆