用于编译共享对象库文件的简单makefile

时间:2023-01-21 13:28:24

I need a very simple makefile to compile a shared object library file (*.so). I also need to know how to pass optimization parameters like -O2 and -O3. I tried to search for simple examples using google, but all examples are twisted.

我需要一个非常简单的makefile来编译一个共享对象库文件(* so)。我还需要知道如何传递优化参数,比如-O2和-O3。我尝试用谷歌搜索简单的例子,但是所有的例子都是扭曲的。

I don't need to create any version like *.so.1.0 but only a simple *.so file. My project would have multiple files, so I need an example which compiles multiple files.

我不需要创建任何版本,比如*.so.1.0,只需要创建一个简单的*。所以文件。我的项目将有多个文件,所以我需要一个编译多个文件的示例。

1 个解决方案

#1


3  

The simplest makefile that I can think of that does what you want:

我能想到的最简单的makefile实现了您想要的:

CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
    g++ -shared $^ -o $@

In the alternative, you may want to use more of make's built-in rules and variables:

在另一种选择中,您可能希望使用更多make的内置规则和变量:

CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
    $(LINK.cc) -shared $^ $(LOADLIBES) $(LDLIBS) -o $@

#1


3  

The simplest makefile that I can think of that does what you want:

我能想到的最简单的makefile实现了您想要的:

CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
    g++ -shared $^ -o $@

In the alternative, you may want to use more of make's built-in rules and variables:

在另一种选择中,您可能希望使用更多make的内置规则和变量:

CXXFLAGS += -fPIC
CXXFLAGS += -O3
x.so: x.o y.o
    $(LINK.cc) -shared $^ $(LOADLIBES) $(LDLIBS) -o $@