0
  1. I want to compile my own dynamic link library(.so), we assume its name is worker_lib.so.

  2. worker_lib.so depends on other dynamic link libraries such as libprotobuf-lite.solibprotobuf.so and libprotoc.so. These libraries are in a directory(deps/lib).

  3. How can I compile my own dynamic link library(worker_lib.so) including thees dependent libraries(libprotobuf-lite.solibprotobuf.so...)

  4. Note I want to accomplish it in cmd or Makefile.

  5. In summary, I want to compile ps-lite(https://github.com/dmlc/ps-lite) to .so.

The related post is Linking a shared library with another shared lib in linux. But the answer can not solve my question.

(info "ps start")
ifdef config
$(info "isconfig")
include $(config)
endif

include make/ps.mk

ifndef CXX
CXX = g++
endif

ifndef DEPS_PATH
DEPS_PATH = $(shell pwd)/deps
endif


ifndef PROTOC
PROTOC = ${DEPS_PATH}/bin/protoc
endif

INCPATH = -I./pssrc -I./include -I$(DEPS_PATH)/include
CFLAGS = -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions $(INCPATH) $(ADD_CFLAGS)


include make/deps.mk


lint:
    python tests/lint.py ps all include/ps pssrc

ps: build/libps.a
pslib: build/lib/worker_lib.so

PS_OBJS = $(addprefix build/, customer.o postoffice.o van.o meta.pb.o)
PS_LIB_SRC = $(addprefix build/, customer.o postoffice.o van.o meta.pb.o worker.o)

build/lib/worker_lib.so: $(PS_LIB_SRC)
    @mkdir -p build/lib
    $(CXX) -shared -Xlinker --unresolved-symbols=ignore-in-shared-libs $^ -o $@ $(CFLAGS)

build/libps.a: $(PS_OBJS)
    ar crv $@ $(filter %.o, $?)

build/%.o: pssrc/%.cc ${ZMQ} pssrc/meta.pb.h
    @mkdir -p $(@D)
    $(CXX) $(INCPATH) -std=c++0x -MM -MT build/$*.o $< >build/$*.d
    $(CXX) $(CFLAGS) -c $< -o $@

pssrc/%.pb.cc pssrc/%.pb.h : pssrc/%.proto ${PROTOBUF}
    $(PROTOC) --cpp_out=./pssrc --proto_path=./pssrc $<

-include build/*.d
-include build/*/*.d

I want to make pslib to compile worker_lib.so. But when I make pslib, I meet the following question: error information

other error informations

The error information is as following:

/home/xiaonan/hbsun/hbsun_Athena/deps/bin/protoc --cpp_out=./pssrc --proto_path=./pssrc pssrc/meta.proto
g++ -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include -std=c++0x -MM -MT build/customer.o pssrc/customer.cc >build/customer.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include  -c pssrc/customer.cc -o build/customer.o
g++ -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include -std=c++0x -MM -MT build/postoffice.o pssrc/postoffice.cc >build/postoffice.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include  -c pssrc/postoffice.cc -o build/postoffice.o
g++ -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include -std=c++0x -MM -MT build/van.o pssrc/van.cc >build/van.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include  -c pssrc/van.cc -o build/van.o
g++ -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include -std=c++0x -MM -MT build/meta.pb.o pssrc/meta.pb.cc >build/meta.pb.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include  -c pssrc/meta.pb.cc -o build/meta.pb.o
g++ -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include -std=c++0x -MM -MT build/worker.o pssrc/worker.cc >build/worker.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include  -c pssrc/worker.cc -o build/worker.o
g++ -shared -Xlinker --unresolved-symbols=ignore-in-shared-libs build/customer.o build/postoffice.o build/van.o build/meta.pb.o build/worker.o -o build/lib/worker_lib.so -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -lpthread -finline-functions -I./pssrc -I./include -I/home/xiaonan/hbsun/hbsun_Athena/deps/include
build/van.o: In function `ps::Van::UnpackMeta(char const*, int, ps::Meta*)':
/root/hbsun/hbsun_Athena/pssrc/van.cc:464: undefined reference to `google::protobuf::MessageLite::ParseFromArray(void const*, int)'
build/van.o: In function `google::protobuf::RepeatedField<int>::Get(int) const':
/home/xiaonan/hbsun/hbsun_Athena/deps/include/google/protobuf/repeated_field.h:1182: undefined reference to `google::protobuf::internal::LogMessage::LogMessage(google::protobuf::LogLevel, char const*, int)'
/home/xiaonan/hbsun/hbsun_Athena/deps/include/google/protobuf/repeated_field.h:1182: undefined reference to `google::protobuf::internal::LogMessage::operator<<(char const*)'
/home/xiaonan/hbsun/hbsun_Athena/deps/include/google/protobuf/repeated_field.h:1182: undefined reference to `google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&)'
/home/xiaonan/hbsun/hbsun_Athena/deps/include/google/protobuf/repeated_field.h:1182: undefined reference to `google::protobuf::internal::LogMessage::~LogMessage()'
Alfred
  • 71
  • 5
  • Unlike static libraries, shared libraries are independent much in the same way as executable programs themselves. – Some programmer dude Jul 16 '19 at 12:03
  • As for your problem, what *is it?* You tell us what you want to do, and tell us that there's some problems, but you don't actually say what the problems are. Please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 16 '19 at 12:04
  • I have fixed the description. THX! – Alfred Jul 16 '19 at 12:12
  • Please don't post images of text. Copy-paste the text, as text, into the question. – Some programmer dude Jul 16 '19 at 12:17
  • As for the errors, you don't actually link with the protobuf library. Likes I said above, a shared library is a stand-alone unit much like an executable program. And as such it's linked very much like an executable library (but with the `-shared` option to actually create a shared library). And just like when linking a program, you need to link your shared libraries with its dependencies. – Some programmer dude Jul 16 '19 at 12:19
  • It's much easier to learn how to use GNU autotools, and let libtool handle linking shared libraries for you. – Sam Varshavchik Jul 16 '19 at 12:22
  • Year, I actually want to know how to link the protobuf library in makefile. I think I need to change the following code, but I do not know how to do it. ``` build/lib/worker_lib.so: $(PS_LIB_SRC) ``` – Alfred Jul 16 '19 at 12:24
  • If you were making an actual program and not a shared library, would you know how to do it then? – Some programmer dude Jul 16 '19 at 12:29
  • Perhaps the problem is that you mix the variables of the makefile: `CFLAGS` are supposed to be C compiler flags only, `CXXFLAGS` are C++ compiler flags, `LDFLAGS` are linker flags, and `LDLIBS` are libraries to link with. If you set these variables correctly, and set up your dependencies correctly, you don't actually need to write the commands to use at all, the `make` program will use [*implicit rules*](https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html#Implicit-Rules) to build your programs and libraries. – Some programmer dude Jul 16 '19 at 12:30
  • Did you know that it can compile and output the dependency file at the same time? – Maxim Egorushkin Jul 16 '19 at 13:40
  • Can you explain it in details? @Maxim Egorushkin – Alfred Jul 16 '19 at 13:53
  • @Alfred Replace `$(CXX) $(CFLAGS) -c $< -o $@` with `$(CXX) $(CFLAGS) -c $< -o $@ -MD -MF ${@:%.o=%.d}`. – Maxim Egorushkin Jul 16 '19 at 14:00
  • The error still exits and nothing change. @Maxim Egorushkin – Alfred Jul 16 '19 at 14:08
  • That wasn't about fixing the error (that would be an answer, not a comment), rather using one compiler command instead of two. – Maxim Egorushkin Jul 16 '19 at 14:19

0 Answers0