cmake 动态库构建及使用(c动态库)

一、c库文件增加

增加Core.h 头文件

#函数声明
int add(int a,int b);

增加Core.cpp文件

#include "Core.h"

int add(int a, int b)
{
	return a+b;
}

二、CMakeLists.txt规则增加

   规则见代码

#cmake 版本要求
cmake_minimum_required(VERSION 2.8)

#增加头文件路径
include_directories(.)

#增加源文件路径
aux_source_directory(. SRC_LIST1)

#增加库目标
add_library(core SHARED  ${SRC_LIST1})

三、编译生成动态库

[root@ dync_core_lib]# ls
build  CMakeLists.txt  Core.cpp  Core.h
[root@ dync_core_lib]# cd build/
[root@ build]# cmake ..
-- The C compiler identification is GNU 5.2.1
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/project/dync_core_lib/build
[root@ build]# make
Scanning dependencies of target core
[100%] Building CXX object CMakeFiles/core.dir/Core.cpp.o
Linking CXX shared library libcore.so
[100%] Built target core
[root@ build]# ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  libcore.so  Makefile

四、项目使用libcore.so动态库

 把Core.h增加到项目的include中

 把libcore.so动态库增加到项目中

 举例:

          把Core.h放到项目的Include/Core中

          把libcore.so放到项目的Lib中

── build
├── CMakeLists.txt
├── Include
│   ├── Core
│   │   └── Core.h
│   └── Libuv
│       ├── Libuv.cpp
│       ├── Libuv.h
│       └── UVInclude
│           ├── uv
│           │   ├── aix.h
│           │   ├── bsd.h
│           │   ├── darwin.h
│           │   ├── errno.h
│           │   ├── linux.h
│           │   ├── os390.h
│           │   ├── posix.h
│           │   ├── sunos.h
│           │   ├── threadpool.h
│           │   ├── tree.h
│           │   ├── unix.h
│           │   ├── version.h
│           │   └── win.h
│           └── uv.h
├── Lib
│   ├── libcore.so
│   └── libuv.so
└── Main.cpp

CMakeLists.txt规则

cmake_minimum_required(VERSION 2.8)


#项目名称
project(main)

#向编译单元添加包含目录的路径。这允许源文件包含来自指定目录的头文件
include_directories(Include)

#用于将指定目录下的所有源文件列表赋值给一个变量
aux_source_directory(Include/Libuv SRC_LIST1)

#从指定目录查找对应的库
find_library(LIB libuv.so ./Lib)
find_library(LIB1 libcore.so ./Lib)

if(LIB)
	message("find libuv.so")
else()
	message("not find libuv.so")
endif()

#添加一个可执行目标以及它的源文件
add_executable(main Main.cpp ${SRC_LIST1})

#为指定的目标添加链接库, 这里增加libuv.so库
target_link_libraries(main ${LIB} ${LIB1})

Main.cpp使用库

#include<iostream>
#include "Libuv/Libuv.h"
#include "Core/Core.h"

using namespace std;

int main(){
	int result = add(1, 2);
	cout<<"result:"<<result<<endl;
	Libuv libuv;
	libuv.Init();
	
	
	string input;
	while(true){
		cout<<"enter cmd( input 'exit' quit process)"<<endl;
		getline(cin, input);
		if( input == "exit"){
			cout<<"exit process"<<endl;
			break;
		} else
		{
			cout<<"unknow cmd:"<<input<<endl;
		}
	}

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值