c – 将Makefile转换为CMake

我正在尝试将Makefile项目转换为cmake.

我的项目有一个可执行文件,它可以构建并链接到内部共享库和外部共享库(可执行文件中有两个依赖项).

这是我的旧Makefile(通用):

Include = -I$(PROJ_DIRECTORY)/include -I/${EXTERN}/lnInclude
Library = -L$(PROJ_DIRECTORY)/build -lshared  -L$(EXTERN_LIBBIN) -lextern
CPPFLAGS    = -D_GLIBCXX_DEBUG -DProjectRescource=$(PROJ_DIRECTORY)/resources -O0 -g3 -Wall -c -fpermissive -std=c++0x -fPIC -MMD -MP
CCC     = g++
TARGET      = ExecutableName

all: $(TARGET)

clean:
    rm -f $(TARGET).o $(TARGET).d $(TARGET)

$(TARGET) : $(TARGET).o
    $(CCC) -o $(TARGET) $(TARGET).o $(Library) 

$(TARGET).o : 
    $(CCC) $(Include) $(CPPFLAGS) $(TARGET).cpp

这是我对CMakeLists.txt的尝试(概括):

include_directories(${PROJECT_SOURCE_DIR}/include/)
add_executable(test1 test1.cpp)
set (CMAKE_CXX_FLAGS "-D_GLIBCXX_DEBUG -DProjectRescource=${PROJECT_SOURCE_DIR}/resources -O0 -g3 -Wall -fpermissive -std=c++0x -fPIC -MMD -MP")
include_directories($ENV{EXTERN_INCLUDE}/lnInclude)
target_link_libraries(test1 "$ENV{EXTERN_LIBBIN}/libextern.so" Project)

当我运行可执行文件时,调用include目录中定义的函数返回-nan.有谁知道为什么?

最佳答案 问题的原因是由于共享库的链接顺序.

我能够通过将CMakeLists.txt的最后一行更改为:

target_link_libraries(test1 Project "$ENV{EXTERN_LIBBIN}/libextern.so")
点赞