cmake:进入链接阶段时找不到共享库

时间:2018-06-22 04:30:26

标签: makefile cmake linker cross-compiling ld

我正在尝试将Makefile移至cmake。问题是,当涉及到链接阶段时,它找不到共享库。

我的cmake是:

set(PROJECT_LINK_LIBS libsr_tps.so)
link_directories(absolute/path/to/the/library)

我也尝试过:

find_library(PROJECT_LINK_LIBS NAMES sr_tps PATHS "${PROJECT_SOURCE_DIR}/lib")

然后:

add_executable(project ${SOURCE})
target_link_libraries(project ${PROJECT_LINK_LIBS})

可以成功交叉编译源。但是在编译后,它始终显示:

/absolute/path/to/ld: cannot find -lsr_tps
collect2: ld returned 1 exit status

鉴于libsr_tps.so已经预先交叉编译。

Makefile在编译和生成最终可执行文件时都可以正常工作。

许多小时以来,我一直在寻找潜在的问题。解决方案只是行不通。

可能是什么问题?

谢谢。


更新:

make的构建方式:

  1. 设置一些环境参数,即一些export
  2. Makefile:

    LDFLAGS += -L./lib
    CFLAGS += -Wall -Wno-pointer-sign
    LDLIBS = -lsr_tps
    all: $(TARGET)
    $(TARGET): $(OBJS) $(OBJS_ROS) 
         $(CC) $(LDFLAGS) $(OBJS) $(OBJS_ROS) $(LDLIBS) -o $(TARGET) 
         cp $^ ./$(BINARY)/$(OBJ_DIR) 
         mv $@ ./$(BINARY) 
    
    .c.o: $(CC) $(CFLAGS) -o $@ -c $<
    

更新2:

这是我的Makefile:

TARGET = ros
LDFLAGS += -L./lib
CFLAGS += -Wall -Wno-pointer-sign -DWSU_5001 -I./include -I./include/WSU-5001_include -I./include/J2735 -I./ -pthread -O
LDLIBS = -lsr_tps -lrisapi -lrt -lipc -lm -ldot2 -ldot3 -lcrypto -lgps -ltpsapi
SHARED_FLAGS = -fPIC -shared

BINARY= bin
SRC_DIR = ./src
J2735_DIR = ./src/J2735
#OBJ_DIR = ./binary/obj
OBJ_DIR = obj

OBJS_TEST=${ASN_MODULE_SOURCES:.c=.o} ${ASN_CONVERTER_SOURCES:.c=.o}
OBJS=${ASN_MODULE_SOURCES:.c=.o}
OBJS_ROS = ros.o tx.o rx.o util.o config.o

all: $(TARGET)
    mkdir -p $(BINARY)
    mkdir -p $(BINARY)/$(OBJ_DIR)

$(TARGET): $(OBJS) $(OBJS_ROS)
    $(CC) $(LDFLAGS) $(OBJS) $(OBJS_ROS) $(LDLIBS) -o $(TARGET) 
    cp $^ ./$(BINARY)/$(OBJ_DIR)
    mv $@ ./$(BINARY)

# $(TARGET): $(OBJS)
#   $(CC) $(OBJS) $(CFLAGS) -o $@

.SUFFIXES:
.SUFFIXES: .c .o

.c.o:
    $(CC) $(CFLAGS) -o $@ -c $<

执行make后的命令行:

path/to/powerpc-e300c3-linux-gnu-gcc -L./lib <all .o files> -lsr_tps -lrisapi -lrt -lipc -lm -ldot2 -ldot3 -lcrypto -lgps -ltpsapi -o ros

下面是我的CMakeList:

cmake_minimum_required(VERSION 3.5.1)
project(Denso-ROS)

set(CMAKE_SYSTEM_NAME Denso-linux)
set(CMAKE_SYSTEM_PROCESSOR "^(powerpc|ppc)")

set(GCC_COVERAGE_COMPILE_FLAGS "-static -Wall -Wno-pointer-sign -DWSU_5001 -pthread -O")

#################
#problem

#set(GCC_COVERAGE_LINK_FLAGS "libsr_tps.so -lrisapi -lrt -lipc -lm -ldot2 -ldot3 -lcrypto -lgps -ltpsapi")
#set(GCC_COVERAGE_LINK_FLAGS "-L./lib")

#set(CMAKE_LINK_LIBRARY_FLAG "-lsr_tps -lrisapi -lrt -lipc -lm -ldot2 -ldot3 -lcrypto -lgps -ltpsapi")

#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )

set(PROJECT_LINK_LIBS libsr_tps.so)
link_directories(home/yufeiyan/DENSO-WSU5K1-SDK_VM_Ubuntu_Peloton/Denso/lib)

#################
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )

set(tools ${PROJECT_SOURCE_DIR}/toolchain/bin)
set(CMAKE_C_COMPILER ${tools}/powerpc-e300c3-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${tools}/powerpc-e300c3-linux-gnu-g++)

find_library(DENSO_LIB NAMES sr_tps PATHS "${PROJECT_SOURCE_DIR}/lib")  
message(STATUS "${DENSO_LIB}")

include_directories(
    ${PROJECT_SOURCE_DIR}
    include
    include/J2735
    include/WSU-5001_include)


file(GLOB SOURCE ${PROJECT_SOURCE_DIR}/*.c src/*.c src/J2735/*.c)
list(REMOVE_ITEM SOURCE ${PROJECT_SOURCE_DIR}/src/J2735/converter-example.c)
list(REMOVE_ITEM SOURCE ${PROJECT_SOURCE_DIR}/src/J2735/test.c)

#add_executable(ros ${SOURCE1} ${SOURCE2} ${SOURCE3})

add_executable(ros ${SOURCE})
target_link_libraries(ros ${PROJECT_LINK_LIBS})

执行make后的命令行:

path/to/powerpc-e300c3-linux-gnu-gcc   -static -Wall -Wno-pointer-sign -DWSU_5001 -pthread -O <all .o files> -L/path/to/project/lib -lsr_tps -Wl,-rpath, path/to/project/lib path/tp/powerpc-e300c3-linux-gnu/bin/ld: cannot find -lsr_tps

1 个答案:

答案 0 :(得分:0)

非常感谢 Tsyvarev

让我总结一下问题和解决方案:

链接器由于路径错误而找不到该库。我使用了link_directories(absolute/path/to/lib),它返回了一条重复的路径。相反,使用link_directories(${PROJECT_SOURCE_DIR}/lib)解决了这个问题。链接器具有正确的路径并找到库。

与此同时,我删除了-static的{​​{1}}选项。此选项还可能导致链接程序找不到库。