使用cmake首先编译了一个静态库,这个静态库链接其他的动态库(log4cpuls.so)。
然后在编译另外一个动态库的时候遇到如下链接错误:
Linking CXX shared library libfcnetwork.so
/usr/bin/ld: ../ssqhelper/libssqhelper.a(cpu_binder.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
../ssqhelper/libssqhelper.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [fcnetwork/libfcnetwork.so] Error 1
make[1]: *** [fcnetwork/CMakeFiles/fcnetwork.dir/all] Error 2
我已经在cmake文件里加了
SET(CMAKE_CXX_FLAGS "-fPIC")
SET(CMAKE_C_FLAGS "-fPIC")
但还是报这个错误,请问是哪里需要修改吗?报错中提到了cpu_binder.cpp,这个文件里引用了sys/sysinfo.h sched.h pthread.h vector unordered_map 几个头文件。
以下是动态库的cmakelists
PROJECT(fcnetwork)
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -fPIC -ggdb ")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -fPIC")
SET(CMAKE_CXX_FLAGS "-fPIC")
SET(CMAKE_C_FLAGS "-fPIC")
ADD_DEFINITIONS(-std=c++11)
INCLUDE_DIRECTORIES(../3rd/log4cplus_1_2_1/include ../3rd/libevent_2_1_8/include include)
LINK_DIRECTORIES(../3rd/libevent_2_1_8/debug ../3rd/log4cplus_1_2_1/debug ./ssqhelper)
AUX_SOURCE_DIRECTORY(src DIR_SRC)
ADD_LIBRARY(${PROJECT_NAME} SHARED ${DIR_SRC})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ssqhelper log4cplus event-2.1 event_core-2.1 event_extra-2.1 event_pthreads-2.1 pthread ld)
下面是静态库的cmakelists
cmake_minimum_required(VERSION 2.8)
PROJECT(ssqhelper)
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb ")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
ADD_DEFINITIONS(-std=c++11)
INCLUDE_DIRECTORIES(../3rd/log4cplus_1_2_1/include)
LINK_DIRECTORIES(../3rd/log4cplus_1_2_1/debug)
INCLUDE_DIRECTORIES(include)
AUX_SOURCE_DIRECTORY(src DIR_SRC)
ADD_LIBRARY(ssqhelper STATIC ${DIR_SRC})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} log4cplus)