I am quite new to c++ Makefile. Recently, I am trying to re-implement the dense trajectory algorithm introduced by this paper: https://lear.inrialpes.fr/people/wang/dense_trajectories
However, I was struggling with Makefile that they provided for quite a while. The Makefile is as follows:
# set the binaries that have to be built
TARGETS := DenseTrack Video
# set the build configuration set
BUILD := release
#BUILD := debug
# set bin and build dirs
BUILDDIR := .build_$(BUILD)
BINDIR := $(BUILD)
# libraries
LDLIBS = $(addprefix -l, $(LIBS) $(LIBS_$(notdir $*)))
LIBS := \
opencv_core opencv_highgui opencv_video opencv_imgproc \
avformat avdevice avutil avcodec swscale
# set some flags and compiler/linker specific commands
CXXFLAGS = -pipe -D __STDC_CONSTANT_MACROS -D STD=std -Wall $(CXXFLAGS_$(BUILD)) -I. -I/usr/local/include -I/home/wei/ffmpeg_build/include
CXXFLAGS_debug := -ggdb
CXXFLAGS_release := -O3 -DNDEBUG -ggdb
LDFLAGS = -L/usr/local/lib -L/home/wei/ffmpeg_build/lib -pipe -Wall $(LDFLAGS_$(BUILD))
LDFLAGS_debug := -ggdb
LDFLAGS_release := -O3 -ggdb
include make/generic.mk
I can verify that my opencv2.4.2 and ffmpeg5.4.0 are successfully installed in Ubuntu 16.04, by running an example using those two libraries.
For opencv:
The lib path is: /usr/local/lib
libavcodec.a libopencv_gpu.so.2.4
libopencv_photo.so.2.4
libavdevice.a libopencv_gpu.so.2.4.2 libopencv_photo.so.2.4.2
libavfilter.a libopencv_highgui.so libopencv_stitching.so
libavformat.a libopencv_highgui.so.2.4 libopencv_stitching.so.2.4
libavutil.a libopencv_highgui.so.2.4.2 libopencv_stitching.so.2.4.2
libopencv_calib3d.so libopencv_imgproc.so libopencv_ts.so
libopencv_calib3d.so.2.4 libopencv_imgproc.so.2.4 libopencv_ts.so.2.4
libopencv_calib3d.so.2.4.2 libopencv_imgproc.so.2.4.2 libopencv_ts.so.2.4.2
libopencv_contrib.so libopencv_legacy.so libopencv_video.so
libopencv_contrib.so.2.4 libopencv_legacy.so.2.4 libopencv_video.so.2.4
libopencv_contrib.so.2.4.2 libopencv_legacy.so.2.4.2 libopencv_video.so.2.4.2
libopencv_core.so libopencv_ml.so libopencv_videostab.so
libopencv_core.so.2.4 libopencv_ml.so.2.4 libopencv_videostab.so.2.4
libopencv_core.so.2.4.2 libopencv_ml.so.2.4.2 libopencv_videostab.so.2.4.2
libopencv_features2d.so libopencv_nonfree.so libpostproc.a
libopencv_features2d.so.2.4 libopencv_nonfree.so.2.4 libswresample.a
libopencv_features2d.so.2.4.2 libopencv_nonfree.so.2.4.2 libswscale.a
libopencv_flann.so libopencv_objdetect.so pkgconfig
libopencv_flann.so.2.4 libopencv_objdetect.so.2.4 python2.7
libopencv_flann.so.2.4.2 libopencv_objdetect.so.2.4.2 python3.5
libopencv_gpu.so libopencv_photo.so
The include path is: usr/local/include:
libavcodec libavfilter libavutil libswresample opencv
libavdevice libavformat libpostproc libswscale opencv2
For ffmpeg:
The lib path is: /home/user/ffmpeg_build/lib
libavcodec.a libavfilter.a libavutil.a libfdk-aac.la libswresample.a libx265.a
libavdevice.a libavformat.a libfdk-aac.a libpostproc.a libswscale.a pkgconfig
The include path is: /home/user/ffmpeg/include
fdk-aac libavdevice libavformat libpostproc libswscale x265.h
libavcodec libavfilter libavutil libswresample x265_config.h
So when I make it with Makefile, I did not get error but it seems linking is working properly, nor is the compiled output.
I tried to directly compile with g++ command.
sudo g++ -o Video -pipe -D __STDC_CONSTANT_MACROS -D STD=std -Wall -I. -I/usr/local/include -O3 -DNDEBUG -ggdb -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc -lavformat -lavdevice -lavutil -lavcodec -lswscale *.h Video.cpp
However,it returns errors as:
/tmp/ccjYmwI4.o: In function `main':
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:33: undefined reference to `cvCreateFileCapture'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:48: undefined reference to `cvQueryFrame'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:57: undefined reference to `cvCopy'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:60: undefined reference to `cvShowImage'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:61: undefined reference to `cvWaitKey'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:53: undefined reference to `cvCreateImage'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:70: undefined reference to `cvDestroyWindow'
/home/wei/Documents/dt/dense_trajectory_release_v1.2/Video.cpp:41: undefined reference to `cvNamedWindow'
collect2: error: ld returned 1 exit status
I have been working on this issue for several days. Any help or suggestions would be really appreciated.
Thanks
The problem comes from your linker call:
The library options (-l) must come after the code to compile or to link:
Correct:
# code in foo.cpp need function in libmylib.so
g++ foo.cpp -lmylib
# code in foo.o need function in libmylib.so
g++ foo.o -lmylib
Incorrect:
# code in foo.cpp need function in libmylib.so
g++ -lmylib foo.cpp
# code in foo.o need function in libmylib.so
g++ -lmylib foo.o
Related
How should I modify Makefiles of DPDK to support c++ compilation? I tried by adding CFLAGS += -lstdc++ to the Makefile of the helloworld example but it seems not working. Is there a more standard way to do that?
Edited:
I'm using the makefile of helloworld example in DPDK 20.08 with some small modifications. I'm building it on ubuntu 20.04 ,and which is not cross-compilation. The DPDK is built with dpdk-setup script and not meson. The makefile is
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2010-2014 Intel Corporation
# binary name
APP = rss_helper
# all source are stored in SRCS-y
# SRCS-y := main.c
SRCS-y := test.cpp
# Build using pkg-config variables if possible
ifeq ($(shell pkg-config --exists libdpdk && echo 0),0)
all: shared
# all: static
.PHONY: shared static
shared: build/$(APP)-shared
ln -sf $(APP)-shared build/$(APP)
static: build/$(APP)-static
ln -sf $(APP)-static build/$(APP)
PKGCONF ?= pkg-config
PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
LDFLAGS_SHARED = $(shell $(PKGCONF) --libs libdpdk)
LDFLAGS_STATIC = $(shell $(PKGCONF) --static --libs libdpdk)
build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build
$(CC) $(CFLAGS) $(SRCS-y) -o $# $(LDFLAGS) $(LDFLAGS_SHARED)
build/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build
$(CC) $(CFLAGS) $(SRCS-y) -o $# $(LDFLAGS) $(LDFLAGS_STATIC)
build:
#mkdir -p $#
.PHONY: clean
clean:
rm -f build/$(APP) build/$(APP)-static build/$(APP)-shared
test -d build && rmdir -p build || true
else
ifeq ($(RTE_SDK),)
$(error "Please define RTE_SDK environment variable")
endif
# Default target, detect a build directory, by looking for a path with a .config
RTE_TARGET ?= $(notdir $(abspath $(dir $(firstword $(wildcard $(RTE_SDK)/*/.config)))))
include $(RTE_SDK)/mk/rte.vars.mk
CPPFLAGS += -O3
CPPFLAGS += $(WERROR_FLAGS)
CPPFLAGS += -DALLOW_EXPERIMENTAL_API
CPPFLAGS += -lstdc++
include $(RTE_SDK)/mk/rte.extapp.mk
endif
I changed the name of the source file and flags. The source file test.cpp contains only iostream header and an empty main function (just for test). There are two errors:
can't find the test.cpp with cpp on. It works find when replace test.cpp in makefile with test.c while keeping the actual source file name as test.cpp.
LD rss_helper
gcc: error: test.cpp: No such file or directory
make[1]: *** [/home/syk/dpdk-20.08/mk/rte.app.mk:456: rss_helper] Error 1
make: *** [/home/syk/dpdk-20.08/mk/rte.extapp.mk:15: all] Error 2
Error for LD like below
g++ -O3 -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wpointer-arith -Wcast-align -Wnested-externs -Wcast-qual -Wformat-nonliteral -Wformat-security -Wundef -Wwrite-strings -Wdeprecated -Wno-missing-field-initializers -Wimplicit-fallthrough=2 -Wno-format-truncation -Wno-address-of-packed-member -DALLOW_EXPERIMENTAL_API -lstdc++ -c -o test.o /home/syk/loadbalancing/rss_helper_demo/test.cpp
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
cc1plus: warning: command line option ‘-Wmissing-prototypes’ is valid for C/ObjC but not for C++
cc1plus: warning: command line option ‘-Wold-style-definition’ is valid for C/ObjC but not for C++
cc1plus: warning: command line option ‘-Wnested-externs’ is valid for C/ObjC but not for C++
LD rss_helper
/usr/bin/ld: test.o: in function `_GLOBAL__sub_I_main':
test.cpp:(.text.startup+0x20): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: test.cpp:(.text.startup+0x27): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
make[1]: *** [/home/syk/dpdk-20.08/mk/rte.app.mk:456: rss_helper] Error 1
make: *** [/home/syk/dpdk-20.08/mk/rte.extapp.mk:15: all] Error 2
I tried to resolve it by added -lstdc++ but still had it.
Edited-2:
The source file:
#include <iostream>
#include <rte_eal.h>
using namespace std;
class A{
int a;
};
int
main(void){
return 0;
}
It can't include iostream and rte_eal either.
there are 2 possible ways to solve this issue
set environment set CC=g++. Then execute make
edit Makefile to add CC = g++
with these changes I am able build and run the sample code.
linux-vdso.so.1 (0x00007fff0a3e6000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1389440000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f138904f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1388cb1000)
/lib64/ld-linux-x86-64.so.2 (0x00007f13899cb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1388a99000)
and
ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=6e3fb6ed5f48b8f66fe4c05118f7b6bf6a9a1235, not stripped
You need to modify the makefile inorder to adapt C++:
You need to change CFLAGS to CPPFLAGS
See below reference example:
ifeq ($(RTE_SDK),)
$(error "Please define RTE_SDK environment variable")
endif
# Default target, can be overriden by command line or environment
RTE_TARGET ?= x86_64-native-linuxapp-gcc
include $(RTE_SDK)/mk/rte.vars.mk
# binary name
APP = dpdkrecv
# all source are stored in SRCS-y
SRCS-y := main.c syncshm.c cqf.cpp countingquotientfilter.cpp murmurhash3.cpp
CPPFLAGS += -O3 -mbmi2
CFLAGS += $(WERROR_FLAGS)
include $(RTE_SDK)/mk/rte.extapp.mk
Please share the Makefile,
I am adding
#include algorithm
dpdk ver: dpdk-stable-18.11.2 ,
build with
sudo make install T=x86_64-native-linuxapp-gcc DESTDIR=install
I am creating client-server application, using gRPC. So far I was not using TLS encryption. Now I want to enable it, and strangely I get this error. It seems, at least to me, that this is a linker error. What would be best ways to solve it?
CLion doesn't highlight anything, so I assumed everything was syntactically OK, but when compiling I got that error.
/usr/bin/ld: CMakeFiles/projectname.dir/main.cpp.o: in function `grpc::SslServerCredentials(grpc::SslServerCredentialsOptions const&)':
/home/username/projectname/third_party/grpc/include/grpcpp/security/server_credentials.h:60: undefined reference to `grpc_impl::SslServerCredentials(grpc::SslServerCredentialsOptions const&)'
collect2: error: ld returned 1 exit status
This is a makefile that I use to generate C++ code from gRPC specification:
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-lgrpc++_reflection\
-ldl
else
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
endif
PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
PROTOS_PATH = ./
vpath %.proto $(PROTOS_PATH)
%.grpc.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
%.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
clean:
rm -f *.o *.pb.cc *.pb.h
Then problem was solved like this:
I had to chage grpc++_unsecure to grpc++ under target_link_libraries in CMakeLists.txt cmake build configuration file.
I had forgotten/not thought about this at first.
target_link_libraries(bita_server
pqxx
sodium
protobuf::libprotobuf
# grpc++_unsecure
grpc++
SQLiteCpp
sqlite3
pthread
dl
${_PROTOBUF_LIBPROTOBUF}
)
In CMakeLists.txt under grpc folder, there are many target_link_libraries. which one to edit in order to include the change.
I try to install magick R package but I get error:
g++ -std=gnu++11 -shared -L/path/apps/R/3.5.1-20180807-test/x86_64-linux-2.6-rhel6/lib64/R/lib -L/path/apps/R/static-zlib-gcc6.3/x86_64-linux-2.6-rhel6/lib -Wl,--exclude-libs,libz.a -Wl,--exclude-libs,libbz2.a -Wl,--exclude-libs,libcurl.a -Wl,--exclude-libs,libpcre.a -Wl,--as-needed -o magick.so RcppExports.o animation.o attributes.o base.o color.o composite.o config.o convolve.o device.o edit.o fonts.o options.o properties.o resize.o transformations.o -L/path/apps/R/static-zlib-gcc6.3/x86_64-linux-2.6-rhel6/lib/ /path/apps/R/static-zlib-gcc6.3/x86_64-linux-2.6-rhel6/lib/libcurl.a /path/apps/R/static-zlib-gcc6.3/x86_64-linux-2.6-rhel6/lib/libz.a -Wl,--exclude-libs,libz.a -Wl,--exclude-libs,libcurl.a -lidn -lssl -lcrypto -lldap -lrt -lcairo -L/path/apps/mysql/5.7.11/x86_64-linux-2.6-rhel6/lib -lmysqlclient -lpthread -lm -lrt -ldl -L/path/apps/R/static-zlib-gcc6.3/x86_64-linux-2.6-rhel6/lib -lxml2 -lz -lm -ldl -L/path/apps/V8/3.14/x86_64-linux-2.6-rhel6/lib64 -L/path/apps/openmpi/1.8.1/x86_64-linux-2.6-rhel6/gnu/lsf/lib -L/path/apps/imagemagick/7.0.7-8/x86_64-linux-2.6-rhel6/lib/pkgconfig -lMagick++-6.Q16 -L/path/apps/R/3.5.1-20180807-test/x86_64-linux-2.6-rhel6/lib64/R/lib -lR
/usr/bin/ld: cannot find -lMagick++-6.Q16
collect2: error: ld returned 1 exit status
make: *** [magick.so] Error 1
ERROR: compilation failed for package ‘magick’
My LIB_DIR and INCLUDE_DIR looks like that:
export LIB_DIR="$STATIC_LIB/lib/ $STATIC_LIB/lib/libcurl.a \
$STATIC_LIB/lib/libz.a -Wl,--exclude-libs,libz.a \
-Wl,--exclude-libs,libcurl.a -lidn -lssl -lcrypto -lldap -lrt \
-lcairo $MYSQL_LIBS $XML2_LIBS -L$V8_LIBS \
-L$MPI_HOME/lib \
-L/path/apps/imagemagick/7.0.7-8/x86_64-linux-2.6-rhel6/lib/pkgconfig"
export INCLUDE_DIR="$STATIC_LIB/include -I/usr/include/cairo \
-I/usr/include/pixman-1 -I/usr/include/freetype2 \
-I/usr/include/libpng12 \
-I$MPI_HOME/include \
-I/path/apps/imagemagick/7.0.7-8/x86_64-linux-2.6-rhel6/include/ImageMagick-7"
include contains:
ls /path/apps/imagemagick/7.0.7-8/x86_64-linux-2.6-rhel6/include/ImageMagick-7
Magick++ MagickCore Magick++.h MagickWand
I found this question but my lib directory contains similar set of files:
ls /path/apps/imagemagick/7.0.7-8/x86_64-linux-2.6-rhel6/lib/
ImageMagick-7.0.7 libMagickCore-7.Q16HDRI.so.4
libMagick++-7.Q16HDRI.a libMagickCore-7.Q16HDRI.so.4.0.0
libMagick++-7.Q16HDRI.la libMagickWand-7.Q16HDRI.a
libMagick++-7.Q16HDRI.so libMagickWand-7.Q16HDRI.la
libMagick++-7.Q16HDRI.so.3 libMagickWand-7.Q16HDRI.so
libMagick++-7.Q16HDRI.so.3.0.0 libMagickWand-7.Q16HDRI.so.4
libMagickCore-7.Q16HDRI.a libMagickWand-7.Q16HDRI.so.4.0.0
libMagickCore-7.Q16HDRI.la pkgconfig
libMagickCore-7.Q16HDRI.so
I'm using imagemagick 7 so why it still requires some flag/arg -lMagick++-6.Q16 from version 6?
If pkg-config and Magick++-config are not available, the magick package defaults to ImageMagick v6:
PKG_LIBS="-lMagick++-6.Q16"
So you will have to install version 6. In addition you might also report an issue asking to support version 7 in the case of a installation without pkg-config/Magick++-config.
Alternatively you can try to get pkg-config/Magick++-config working on your machine. If pkg-config/Magick++-config can be used, the flags provided from there are used directly.
I've finally managed to install magick. I compiled ImageMagick v6 from source and installed R package with following set of env variables:
STATIC_LIB="/path/static-zlib-gcc6.3"
IMAGEMAGICK_V6_HOME="/path/ImageMagick-6.9.10-10"
export LIB_DIR="$STATIC_LIB/lib/ \
-L$IMAGEMAGICK_V6_HOME/lib"
export INCLUDE_DIR="$STATIC_LIB/include \
-DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 \
-I$IMAGEMAGICK_V6_HOME/include/ImageMagick-6"
export PKG_CONFIG_PATH="$IMAGEMAGICK_V6_HOME/lib/pkgconfig/:$PKG_CONFIG_PATH"
export LD_LIBRARY_PATH="$IMAGEMAGICK_V6_HOME/lib:$LD_LIBRARY_PATH"
I have opencv3 installed through home-brew and pkg-config can find the linkers too by
pkg-config --cflags --libs opencv
the outputs contains -lopencv_core, but when I add that in Makefile like this
CC=clang++
CFLAGS= -Wall -g -std=c++0x
LFLAGS= -I/usr/local/Cellar/opencv3/3.2.0/include -I/usr/local/Cellar/opencv3/3.2.0/bin -I/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
CFILES=blah.cpp
HFILES=blah.hpp
OFILES=blah.o
all: main
%.o: %.cpp $(HFILES)
$(CC) -c $(CFLAGS) $< -o $# $(LFLAGS)
main: $(OFILES) $(HFILES)
$(CC) $(CFLAGS) $(OFILES) -o main $(LFLAGS)
it says
ld: library not found for -lopencv_core
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How should I link that to gcc?
Assuming you want CFLAGS to be the options for compilation and LFLAGS to
be the options for linkage, the settings:
CFLAGS= -Wall -g -std=c++0x
LFLAGS= -I/usr/local/Cellar/opencv3/3.2.0/include -I/usr/local/Cellar/opencv3/3.2.0/bin -I/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
are confused.
The -I path option is meaningful for compilation and meaningless for linkage. It
tells the preprocessor to search in path for header files that you #include in
the source code. There will be header files in:
/usr/local/Cellar/opencv3/3.2.0/include
There will be no header files in:
/usr/local/Cellar/opencv3/3.2.0/bin
just executables, which are irrelevant to building your program. And there will be no header files in:
/usr/local/Cellar/opencv3/3.2.0/lib
just static and or dynamic libraries.
The library libopencv_core that linker can't find is presumably
in /usr/local/Cellar/opencv3/3.2.0/lib. The way to tell the
linker to search there for the library is:
-L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
So these settings make sense:
CFLAGS := -Wall -g -std=c++0x -I/usr/local/Cellar/opencv3/3.2.0/include
LFLAGS := -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_core
However, to compile and link an opencv program you would probably be better
leaving it to pkg-config to get the options right:
CFLAGS := -Wall -g -std=c++0x $(shell pkg-config --cflags opencv)
LFLAGS := $(shell pkg-config --libs opencv)
The -l option looks in the usual places for the mentioned library. Where and what it looks varies by OS, which you didn't mention. Possibly it can be fixed by adding -L /usr/local/lib or some other path to where you know the libraries are.
I am trying to build my .so library with all dependencies (mostly boost) statically linked. Currently, I can build statically linked static library and dynamically linked shared library:
I would like to add other dependencies to .so library so that it has 20MB and does not require user to install anything. Note that this is just a temporary solution before we upgrade to new Boost 1.55 on production servers.
I define libraries like this in Makefile ($ARCH can be either 32 or 64):
## Multi-arch library locations
LIB32=/usr/lib/i386-linux-gnu/
LIB64=/usr/lib/x86_64-linux-gnu/
LIBDIR:=$(LIB$(ARCH))
##Library directory
LIB=-L $(LIBDIR)
## DYNAMIC
LIBS=-lboost_serialization -lboost_thread -lboost_date_time -lboost_signals -lboost_iostreams -lboost_system -llog4cplus -lcrypto -lssl -lm
## STATIC
SLIBS=$(LIBDIR)libboost_serialization.a $(LIBDIR)libboost_thread.a $(LIBDIR)libboost_date_time.a $(LIBDIR)libboost_signals.a $(LIBDIR)libboost_iostreams.a $(LIBDIR)libboost_system.a $(LIBDIR)liblog4cplus.a
Shared lib with dynamic linking:
This is my shared lib GCC command:
Makefile:
$(CXX) $(CFLAGS) $(INCLUDE) $(LIB) $(LIBS) -shared -Wl,-soname,$(SHARED_LIB_VERSION) -o $(NEW_LIB_DIR)${SHARED_LIB_VERSION} $(OBJ_CPP_DYN) $(OBJ_C_DYN)
Changes into:
g++ -m64 -Wl,--trace -D NDEBUG -I /usr/include/ -I /usr/local/include -L /usr/lib/x86_64-linux-gnu/ -shared -lboost_serialization -lboost_thread -lboost_date_time -lboost_signals -lboost_iostreams -lboost_system -llog4cplus -lcrypto -lssl -lm -Wl,-soname,libLIBNAMEx64.so -o ../Release/libLIBNAMELIBNAMEx64.so ... and much more .o files ...
Linker says:
-lboost_serialization (/usr/lib/x86_64-linux-gnu//libboost_serialization.so)
-lboost_thread (/usr/lib/x86_64-linux-gnu//libboost_thread.so)
-lboost_date_time (/usr/lib/x86_64-linux-gnu//libboost_date_time.so)
-lboost_signals (/usr/lib/x86_64-linux-gnu//libboost_signals.so)
-lboost_iostreams (/usr/lib/x86_64-linux-gnu//libboost_iostreams.so)
-lboost_system (/usr/lib/x86_64-linux-gnu//libboost_system.so)
-llog4cplus (/usr/lib/x86_64-linux-gnu//liblog4cplus.so)
-lcrypto (/usr/lib/x86_64-linux-gnu//libcrypto.so)
-lssl (/usr/lib/x86_64-linux-gnu//libssl.so)
And it works.
Shared lib with static linking:
I thought I can just replace -shared with -static:
g++ -m64 -Wl,--trace -D NDEBUG -I /usr/include/ -I /usr/local/include -L /usr/lib/x86_64-linux-gnu/ -static -lboost_serialization -lboost_thread -lboost_date_time -lboost_signals -lboost_iostreams -lboost_system -llog4cplus -lcrypto -lssl -lm -Wl,-soname,libLIBNAMEx64_static_link.so -o ../Release/libLIBNAMEx64_static_link.so ... and much more .o files ...
But I get undefined reference errors everywhere.
So where do I configure how are dependencies linked with my binary? How can I achieve my goal?
More things I tried (edit)
-Wl,--library:
I also tried passing libraries directly to linker in a very assertive manner:
-Wl,--library=:/usr/lib/x86_64-linux-gnu/libboost_serialization.a
And without : (which prevents searching for .a):
-Wl,--library=/usr/lib/x86_64-linux-gnu/libboost_serialization.a
But I get the error:
/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libboost_serialization.a
Of course, the file exists at that path.
Pass just library names:
Of course, here it's not even interpreted as linking command:
g++: error: libboost_serialization.a: No such file or directory
Interesting is that if I pass full path instead (/usr/lib/x86_64-linux-gnu/libboost_iostreams.a), GCC doubles it:
g++: error: /usr/lib/x86_64-linux-gnu//usr/lib/x86_64-linux-gnu/libboost_signals.a: No such file or directory
Use command man ld, I got this information:
-static
Do not link against shared libraries. It affects library searching for -l options which follow it. This option also implies --unresolved-symbols=report-all. This option can be used with -shared. Doing so means that a shared library is being created but that all of the library's external references must be resolved by pulling in entries from static libraries.
This option is exactly what you want: create a shared library with all dependencies (mostly boost) statically linked.
-l parameter is used to specify the library name, so you should use boost_serialization instead of /path/libboost_serialization.a:
-larchive
If you specify -lcommon, then ld will search its path-list for occurrences of "libcommon.a" for every common specified.
You can use -L parameter many times to specify the library paths when ld try to search static libraries:
-Lsearchdir
For example:
-L/usr/lib/x86_64-linux-gnu/
You could try -Wl,--whole-archive ${your library} -Wl,--no-whole-archive.