c - 安装仅项目非系统版本的 OpenSSL 和 libcrypt 库

标签 c makefile openssl gnu-make

我有一个带有以下 Makefile 的 C 项目:

CC=mpicc

buildsDir: ./builds
    mkdir ./builds

main: ./builds message dh main.c 
    $(CC) -o ./builds/main main.c ./builds/dh.o ./builds/message.o  -g -lcrypto -lssl

dh: ./builds dh.c dh.h
    $(CC) -c -o ./builds/dh.o dh.c -g -lcrypto -lssl

message: message.c message.h
    $(CC) -c -o ./builds/message.o -g ./message.c

run: main
    mpirun -quiet -np 3 xterm -hold -e ./builds/main &

debug: main
    mpirun -quiet -np 3 xterm -e gdb ./builds/main

.PHONY: clean

clean:
    rm -rf ./builds && mkdir ./builds

在我的系统中我有 OpenSSL 1.0.2g:

OpenSSL 1.0.2g  1 Mar 2016
built on: reproducible build, date unspecified
platform: debian-amd64
options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx) 
compiler: cc -I. -I.. -I../include  -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wa,--noexecstack -Wall -DMD32_REG_T=int -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM
OPENSSLDIR: "/usr/lib/ssl"

但是在我的原型(prototype)软件上,由于缺少系统提供的版本,我需要它的更高版本,并且我想避免全局安装它。因此我想使用仅限本地的版本来限制专业项目的范围。我发现these说明为我提供了一个很好的框架。

我还做了第一步,将 is 作为子模块添加到我的项目中:

[submodule "openssl"]
    path = openssl
    url = https://github.com/openssl/openssl.git
    branch = OPENSSL_1_1_1a

在控制台上,我尝试使用以下命令集手动构建它:

cd openssl
./config --prefix=$(pwd)/../builds/openssl --openssldir=$(pwd)/../builds/openssl
make && make test && make install

现在我想更进一步,以某种方式告诉 mpicc 链接位于 builds 目录的构建 openssl lib,但我不知道。

最佳答案

像这样更改你的 makefile 怎么样?

CC=mpicc

# use headers from builds/openssl
CFLAGS := -g -Ibuilds/openssl/include

# look for library in builds/openssl
LDFLAGS := -Lbuilds/openssl/lib
LIBS    := -lcrypto -lssl

.PHONY: all
all: builds/main

builds:
    mkdir -p $@

builds/main: builds/dh.o builds/message.o
builds/main: main.c
    $(CC) $(CFLAGS) -o $@ $< builds/dh.o builds/message.o $(LDFLAGS) $(LIBS)

builds/dh.o: dh.h
builds/dh.o: dh.c
    $(CC) $(CFLAGS) -o $@ -c $<

builds/message.o: message.h
builds/message.o: message.c
    $(CC) $(CFLAGS) -o $@ -c $<

builds/dh.o builds/message.o builds/main: builds

# if you want to build openssl with your makefile...
builds/dh.o builds/message.o builds/main: builds/openssl
builds/openssl: builds
       .... your openssl build command ....

请检查 builds/openssl 的布局以获得正确的包含和库路径。

<小时/>

奖励代码我个人会重组makefile。为什么不让 make 处理繁重的工作呢?

LDFLAGS := -g -Lbuilds/openssl/lib

SRCS := main.c dh.c message.c
OBJS := $(SRCS:%.c=builds/%.o)
...
builds/main: $(OBJS)
    $(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

# do not re-build when "builds" directory contents change
$(OBJS): | builds
$(OBJS): builds/%.o: %.c
    $(CC) $(CFLAGS) -o $@ -c $<

# if you want to build openssl with your makefile...
$(OBJS): builds/openssl

关于c - 安装仅项目非系统版本的 OpenSSL 和 libcrypt 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543348/

相关文章:

C 二进制 : syntax error: `(' unexpected

c - openssl RSA公钥与从C代码读取的 key 不匹配

c++ - 客户端-服务器程序,使用 TCP 从客户端向服务器发送消息时出现问题,反之亦然

c - C 中的文字数组

使用 Swig 结合 C 和 TCL

linux - 加载 ssl_server 的扩展部分时出错

python - SNI(服务器名称指示)与 Tornado

c - C 中的 AF_PACKET 套接字不使用 SOCK_RAW 发送空 UDP 数据包

c++ - Make 无法识别正在修改的文件

linux - 生成文件中使用的文件的运行时确定