在linux下使用cmake+make编译,报"未定义的引用"错误,错误的CMakeLists.txt如下:

cmake_minimum_required(VERSION 2.6)

project(hellocmake)

LINK_DIRECTORIES(
    /home/test/code/lib.debug/Dependence/libuuid-1.0.3
    )

target_link_libraries(${PROJECT}
       /home/test/code/lib.debug/Dependence/libuuid-1.0.3/libuuidd.a
    )

add_executable(${PROJECT_NAME} main.cpp)


target_include_directories(${PROJECT_NAME}
    PRIVATE
        /home/test/code/Dependence/libuuid-1.0.3
    )

main.cpp内容如下:

#include <stdio.h>
#include <uuid/uuid.h>

extern void uuid_generate(uuid_t out);

int main()
{
    uuid_t		buf, tst;
    char		str[100];
    uuid_generate(buf);
    uuid_unparse(buf, str);
    printf("UUID generate = %s\n", str);

    printf("hello,cmake!");
	char ch =getchar();
}

解决办法:将LINK_DIRECTORIES、target_link_libraries移到add_executable后面,使用${PROJECT_NAME},修改后如下:

cmake_minimum_required(VERSION 2.6)

project(hellocmake)

add_executable(${PROJECT_NAME} main.cpp)

LINK_DIRECTORIES(${PROJECT_NAME}
    /home/test/code/lib.debug/Dependence/libuuid-1.0.3
    )

target_link_libraries(${PROJECT_NAME}
       /home/test/code/lib.debug/Dependence/libuuid-1.0.3/libuuidd.a
    )

target_include_directories(${PROJECT_NAME}
    PRIVATE
        /home/test/code/Dependence/libuuid-1.0.3
    )

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐