linux下生成动态库和链接动态库
阅读原文时间:2023年07月12日阅读:1

src/test.h

#ifndef _TEST_H_HH
#define _TEST_H_HH

void print();
#endif

src/test.cpp

#include "test.h"
#include <stdio.h>

void print()
{
    printf("hello mint!\n");
}

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(test)

if(NOT WIN32)
ADD_DEFINITIONS("-std=c++0x -O3")
endif(NOT WIN32)

SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_library(libtest SHARED "src/test.cpp")

在当前文件夹下新建lib文件夹,会生成lib*.so

将生成的Lib和test.h放到新的工程里面

lib.so放在新工程/lib下

test.h放在新工程/include下

新工程调用代码

src/main.cpp

#include "test.h"

int main()
{
    print();
    return 0;
}

CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)

project(usehellolib)

include_directories(

${PROJECT_SOURCE_DIR}/include

)

add_executable(usehellolib src/main.cpp)

target_link_libraries(usehellolib

${PROJECT_SOURCE_DIR}/lib/liblibtest.so

)