5

I am trying to write a CMakeList.txt in Clion under ubuntu 14.04 as follow:

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
        )

when I run it tells me:

/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'

After I searched this I tried some methods:

1:add ldl like this:

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

2:add a target_link_libraries:

target_link_libraries(ify
        dl
        pthread
        )

3:

set(CMAKE_SHARED_LINKER_FLAGS -ldl)

4:add #include <dlfcn.h> in main.cpp

but neither of these methods works.

Then I tried to compile it without clion and wrote a MakeFile in the folder like this:

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

This Makefile works, I think it was because this sentence:LDFLAGS += -lmsc -lrt -ldl -lpthread, but how can I change my My CMakefile.txt in clion, I want it to work in clion. anyone can help me? thanks a lot.

thiiiiiking
  • 1,233
  • 3
  • 12
  • 16

2 Answers2

3

Try this way:

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

Or(better) this way:

target_link_libraries(ify ${catkin_LIBRARIES} libmsc.so libasound.so ${CMAKE_DL_LIBS})
ixSci
  • 13,100
  • 5
  • 45
  • 79
0

According to this add the following to find pthread for cmake 2.8.12:

find_package(Threads REQUIRED)

Then your target_link_libraries should look like this:

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

Hope this helps.

Community
  • 1
  • 1
aichao
  • 7,375
  • 3
  • 16
  • 18
  • Thanks, I cleared the cake cache and tried this method, but got the same error. It is strange. – thiiiiiking Aug 10 '16 at 13:49
  • @thiiiiiking: Is the CMakeLists.txt file in the same directory as the Makefile that works? What version of cmake are you using? When you run cmake, did it complain about not finding pthread or dl? – aichao Aug 10 '16 at 14:17
  • Yes, They are in the same directory. The version of cmake is 2.8.12 and it did not complain about not finding `pthread` or `dl`. – thiiiiiking Aug 11 '16 at 01:13
  • @thiiiiiking: OK, what I wrote before was for cmake 3.1.0+. For your version see [this](http://stackoverflow.com/questions/1620918/cmake-and-libpthread). I'll update the answer. Hopefully, the undefined reference from pthread will go away. Are you still getting undefined references from dl? – aichao Aug 11 '16 at 02:35
  • @thiiiiiking: BTW, you may want to consider updating your cmake to the most recent version. – aichao Aug 11 '16 at 03:17
  • Good advice, I will update cmake. But the undefined reference come again. Thank very much for your patience. – thiiiiiking Aug 11 '16 at 03:47