如何在CMakeList.txt中链接-ldl

时间:2016-08-10 01:32:05

标签: c++ makefile

我正在尝试在ubntu 14.04下在Clion中编写一个CMakeList.txt,如下所示:

cmake_minimum_required(VERSION 2.8.3)
project(ify)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")

## System dependencies are found with CMake's conventions
find_package(Boost REQUIRED)

include_directories(
        ${catkin_INCLUDE_DIRS}
        /usr/local/include
        /usr/include
        include
)
include_directories(
        include/
)
link_directories(
        /usr/local/lib
        /usr/lib
        libs/x64
        /usr/lib/x86_64-linux-gnu
)

#set(SOURCE_FILES main.cpp)
#add_executable(ify ${SOURCE_FILES})
add_executable(ify main.cpp)

target_link_libraries(ify ${catkin_LIBRARIES}
        libmsc.so
        libasound.so
        )

当我跑步时告诉我:

/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlopen'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlclose'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlerror'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `dlsym'
/home/think/ClionProjects/ify/libs/x64/libmsc.so: undefined reference to `pthread_create'

在我搜索完之后,我尝试了一些方法:

1:像这样添加ldl:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -ldl -std=c++11")

2:添加一个target_link_libraries:

target_link_libraries(ify
        dl
        pthread
        )

3:

set(CMAKE_SHARED_LINKER_FLAGS -ldl)

4:在main.cpp中添加#include <dlfcn.h>

但这两种方法都不起作用。

然后我尝试在没有clion的情况下编译它,并在文件夹中写了一个MakeFile:

DIR_INC = ./include
DIR_BIN = ./bin
DIR_LIB = ./libs

TARGET  = demo
BIN_TARGET = $(DIR_BIN)/$(TARGET)

CROSS_COMPILE = 
CFLAGS = -g -Wall -I$(DIR_INC)

#
ifdef LINUX64
LDFLAGS := -L$(DIR_LIB)/x64
else
LDFLAGS := -L$(DIR_LIB)/x86 
endif

LDFLAGS += -lmsc -lrt -ldl -lpthread

OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))

$(BIN_TARGET) : $(OBJECTS)
    $(CROSS_COMPILE)gcc $(CFLAGS) $^ -o $@ $(LDFLAGS)

%.o : %.c
    $(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@
clean:
    @rm -f *.o $(BIN_TARGET)

.PHONY:clean

这个Makefile有效,我认为这是因为这句话:LDFLAGS += -lmsc -lrt -ldl -lpthread,但是如何在clion中更改我的My CMakefile.txt,我希望它能在clion中工作。 有人可以帮帮我吗?非常感谢。

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

<span>there is '+text1+' '+text2+' here.</span>

或者(更好)这样:

target_link_libraries(ify ${catkin_LIBRARIES} libmsc.so libasound.so libdl.so)

答案 1 :(得分:0)

根据this添加以下内容,为cmake 2.8.12找到pthread

find_package(Threads REQUIRED)

然后你的target_link_libraries应该是这样的:

target_link_libraries(ify ${catkin_LIBRARIES}
    libmsc.so
    libasound.so
    rt
    ${CMAKE_DL_LIBS}
    ${CMAKE_THREAD_LIBS_INIT}
)

希望这有帮助。