Dr.Knowitall
Dr.Knowitall

Reputation: 10468

What is the proper way of building a static binary with openssl?

I have the following make file:

CC=g++
CFLAGS=-c -Wall
REST_LIBS = -lssl -lcrypto -lboost_system -lcpprest

all: main

main: static_pack
    g++ -std=c++14 -D DEBUG -Wfatal-errors -static -pthread -I$(basepath)/vendors/cpp-jwt/include -I$(basepath)/vendors/json/include \
    -DTS=\"/ctts.json\" \
    -DCS_PATH=\"/bin\" \
    -DCTFS_ENC=\"/ctfs.enc\" \
    -DUNTAR_PATH=\"/\" \
    -DCLUSTER_PATH=\"/.clusters\" \
     -o run main.cpp \
    libmain.a && \
    rm -rf debpkg/cs/usr/bin/cs debpkg/cs.deb && \
    cp run debpkg/cs/usr/bin/cs && \
    dpkg-deb -b debpkg/cs && \
    mv debpkg/cs.deb .

static_pack: rest.o aes.o random.o
    ar rcs libmain.a random.o aes/aes.o rest/rest.o

rest.o:
    g++ -std=c++14 -Wfatal-errors -c $(REST_LIBS) -o rest/rest.o rest/rest.cpp 


aes.o: random.o
    g++ -std=c++14 -D DEBUG -Wfatal-errors -c -lcrypto -o aes/aes.o random.o aes/aes.cpp

random.o:
    g++ -std=c++14 -Wfatal-errors -c -o random.o random.cpp

If I compile this to be dynamically linked I have no problems. However, when I try static compilation I get tons of errors such as:

aes.cpp:(.text+0x706): undefined reference to `EVP_DecryptInit_ex'
aes.cpp:(.text+0x732): undefined reference to `EVP_DecryptUpdate'
aes.cpp:(.text+0x763): undefined reference to `EVP_CIPHER_CTX_ctrl'
aes.cpp:(.text+0x792): undefined reference to `EVP_DecryptFinal_ex'
aes.cpp:(.text+0x7a1): undefined reference to `EVP_CIPHER_CTX_free'

Essentially none of the symbols are being found. I'm not sure what I need to do now. I've tried build my object files as static to but that fails. I've looked into linking order, but that seems right.

My question boils down to two things:

  1. When static linking other objects, do those objects need to be statically compiled as well + archived?

  2. What is wrong with my setup?

Upvotes: 0

Views: 166

Answers (2)

Dr.Knowitall
Dr.Knowitall

Reputation: 10468

Ok so apparently linking happens in the opposite order than I thought... and I think it's possible I might have not been linking either originally as a fufu mistake.

REST_LIBS = -lboost_filesystem -lboost_system -lcpprest -lssl -lcrypto -ldl
# /usr/local/lib/libyaml-cpp.a

all: main

main: static_pack
    g++ -std=c++14 -D DEBUG -Wfatal-errors -I$(basepath)/vendors/cpp-jwt/include -I$(basepath)/vendors/json/include \
    -DTS=\"/ctts.json\" \
    -DCS_PATH=\"/bin\" \
    -DCTFS_ENC=\"/ctfs.enc\" \
    -DUNTAR_PATH=\"/\" \
    -DCLUSTER_PATH=\"/.clusters\" \
     -o run main.cpp \
    libmain.a $(REST_LIBS) -pthread  && \
    rm -rf debpkg/cs/usr/bin/cs debpkg/cs.deb && \
    cp run debpkg/cs/usr/bin/cs && \
    dpkg-deb -b debpkg/cs && \
    mv debpkg/cs.deb .

Upvotes: 0

joe_chip
joe_chip

Reputation: 2558

You don't need REST_LIBS for your rest.o rule, as it only compiles a source file. You need to pass those libraries to g++ in main rule - as part of it, g++ will call linker.

Upvotes: 2

Related Questions