首页 > 编程语言 >使用CMakeLists.txt创建一个动态库工程Demo给main程序使用

使用CMakeLists.txt创建一个动态库工程Demo给main程序使用

时间:2024-01-04 11:02:26浏览次数:43  
标签:CMakeLists 20 -- Demo Oct neuti main hello


主要需求是把 hello 程序编译动态库,再 main 程序或者第三方程序执行的时候动态加载。

工程目录如下:

$ ls -al *
-rw-r--r-- 1 neuti 197609 352 Oct 20 19:30 CMakeLists.txt

include:
total 1
drwxr-xr-x 1 neuti 197609  0 Oct 20 19:30 ./
drwxr-xr-x 1 neuti 197609  0 Oct 20 19:30 ../
-rw-r--r-- 1 neuti 197609 93 Oct 20 19:30 hello.h

src:
total 2
drwxr-xr-x 1 neuti 197609   0 Oct 20 19:30 ./
drwxr-xr-x 1 neuti 197609   0 Oct 20 19:30 ../
-rw-r--r-- 1 neuti 197609 112 Oct 20 19:30 hello.c
-rw-r--r-- 1 neuti 197609 103 Oct 20 19:30 main.c

include 目录是头文件目录,src 是源文件目录。

CMakeLists.txt 文件内容如下:

cmake_minimum_required(VERSION 3.10)

project(main)

# Use C11
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)

# Add include directories
include_directories(include)

# Add shared lib files
add_library(hello SHARED src/hello.c)

# Add source files
add_executable(main src/main.c)

# Build target
target_link_libraries(main hello)

main 程序中调用 hello 动态库中的 print_hello_world接口:

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

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

hello.c 内容:

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

void print_hello_world(void)
{
    printf("Hello, world!\n");
}

hello.h 内容:

#ifndef HELLO_H
#define HELLO_H

void print_hello_world(void);

#endif /* HELLO_H */

编译工程:

cmake .
make

执行程序:

./main


标签:CMakeLists,20,--,Demo,Oct,neuti,main,hello
From: https://blog.51cto.com/u_13726704/9097051

相关文章

  • Apple Logic Pro 10.7 (Logic Pro 10.7 + MainStage 3.5 (Universal))
    作者:gc,主页:www.sysin.orgLogicPro本领先声夺人,创意一鸣惊人。使用实时循环乐段,以全新方式进行音乐创作和即兴演奏。借助采样器和快速采样器将声音转化为乐器。通过步进音序器来快速制作鼓点节拍和旋律模式。利用LogicRemote在iPad或iPhone上掌控乐曲的创作。LogicProMai......
  • Python pytest.main()运行测试用例
    前言前面一直使用命令行运行pytest用例,本篇来学下使用pytest.main()来运行测试用例pytest.main()args传一个list对象,list里面是多个命令行的参数plugins传一个list对象,list里面是初始化的时候需注册的插件不带参数运行importpytest#等同于命令行执行pytest#默认运行的是......
  • 如何安装 macOS Big Sur 11.0 Beta 虚机,解决 “BiErrorDomain Error 3.” 报错
    作者:https://sysin.org1.获取官方的macOSBeta软件包官网直接下载,双击直接安装:点击下载Betaprofile,通过软件更新下载上述两种方式,都会将InstallmacOSBeta.app放置于/Applications(应用程序)下面。2.安装方式1.升级安装,下载后直接点击“InstallmacOSBeta”2.在当前系统......
  • svelte的一些基础demo
    脚手架Vite:vite是集成了svelte,初始化的时候选择svelte就行了。npminitviteSvelteKit:底层基于vite的更上层框架,类似于nextjs。[email protected]文件结构和vue类似svelte文件是.svelte结尾的文件,比如demo.svelte......
  • IpcMain模块
    方法IpcMain模块有以下方法来侦听事件:ipcMain.on(channel,listener)channel stringlistener Functionevent IpcMainEvent...args any[]监听channel,当新消息到达,将通过listener(event,args…)调用listener。......
  • postgresql数据库报“connections on Unix domain socket "/tmp/.s.PGSQL.5432"?”
    使用postgresql数据库的时候经常遇到的问题:[postgres@test~]$psqlpsql:couldnotconnecttoserver:Nosuchfileordirectory Istheserverrunninglocallyandaccepting connectionsonUnixdomainsocket"/tmp/.s.PGSQL.5432"?现象如上,但是数据库是启动状态,将......
  • Thoughts and ideas about how to apply LLMs in specific domains like clinic/law/f
    ApplyingLLMsinSpecificDomainsAsauniversitystudentwhohasjustcompletedfine-tuningTinyLLaMA-1bwithclinicalinstructiondatausingtheQLoRAmethodandevaluateditontheMedMCQAdataset,Ihavegatheredsomeinsightsandideasonhowtoappl......
  • Halcon reduce_domain和scale_image的作用
    在Halcon中,reduce_domain是用于缩小图像域(ImageDomain)的操作。它的作用是通过指定一个感兴趣区域(ROI,RegionofInterest),将图像数据限制在该区域内,从而实现对图像进行裁剪或者缩小处理。reduce_domain的语法如下:reduce_domain(Image,Region,ReducedImage)其中,Image是输入的原始图......
  • 随机点名demo+文字特效
    上代码:一、HTML部分<div><h2>随机点名</h2><spanid="name"></span></div><buttonid="button-text">点一下不要钱</button>二、CSS部分*{margin:0;padd......
  • spring:Exception in thread "main" java.lang.NoClassDefFoundError: org/springframe
     设置了父类框架<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.10.RELEASE</version><relativePath/><!--l......