cmake工程结构化

This commit is contained in:
jf_HS 2023-12-25 10:55:16 +08:00
parent 7d3cff8d7c
commit 31a8e9cacc
6 changed files with 107 additions and 86 deletions

View File

@ -1,7 +1,21 @@
# cmake 最低版本号要求
cmake_minimum_required(VERSION 3.8)
# 项目信息
project(demo)
# 查找当前目录下的所有文件
# 并将名称保存在DIR_SRC中
aux_source_directory(. DIR_SRC)
# 添加子目录
add_subdirectory(src)
# 添加头文件路径
include_directories(src)
# 指定生成目标
add_executable(demo ${DIR_SRC})
target_link_libraries(demo list)

87
main.c
View File

@ -3,68 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct list_t
{
uint8_t data;
struct list_t *next;
} list_t;
void list_init(struct list_t *list)
{
list->data = 0;
list->next = NULL;
}
void list_add(struct list_t *list, uint8_t data)
{
struct list_t *p = NULL;
p = list;
while (p->next != NULL)
{
p = p->next;
if (p->data == data)
{
return;
}
}
p->next = (struct list_t *)malloc(sizeof(struct list_t));
p->next->data = data;
p->next->next = NULL;
return;
}
void list_print(struct list_t *list)
{
struct list_t *p = NULL;
p = list;
while (p->next != NULL)
{
printf("%02d\n", p->data);
p = p->next;
if (p->next == NULL)
{
printf("%02d\n", p->data);
break;
}
}
}
void list_free(struct list_t *list)
{
struct list_t *p = NULL;
p = list;
while (p->next != NULL)
{
p = p->next;
free(p->next);
p->next = NULL;
if (p->next == NULL)
{
break;
}
}
}
#include "list.h"
void list_demo(void)
{
@ -81,24 +20,16 @@ void list_demo(void)
void main(int arg, char *argv[])
{
#if 0
uint32_t i = 0;
for (i = 0; i < 10; ++i)
{
printf("%02d\n", i);
}
printf("\n");
struct list_t list;
list_init(&list);
printf("Hello World!\n");
list_add(&list, 1);
list_add(&list, 2);
list_add(&list, 3);
list_add(&list, 4);
list_print(&list);
#else
// struct list_t list;
// list_init(&list);
// list_add(&list, 1);
// list_add(&list, 2);
// list_add(&list, 3);
// list_add(&list, 4);
// list_print(&list);
list_demo();
#endif
}

6
src/CMakelists.txt Normal file
View File

@ -0,0 +1,6 @@
# 当前目录下的文件 -> 写入变量
aux_source_directory(. DIR_LIB_SRC)
# 生成链接库
add_library(list ${DIR_LIB_SRC})

58
src/list.c Normal file
View File

@ -0,0 +1,58 @@
#include "list.h"
void list_init(struct list_t *list)
{
list->data = 0;
list->next = NULL;
}
void list_add(struct list_t *list, uint8_t data)
{
struct list_t *p = NULL;
p = list;
while (p->next != NULL)
{
p = p->next;
if (p->data == data)
{
return;
}
}
p->next = (struct list_t *)malloc(sizeof(struct list_t));
p->next->data = data;
p->next->next = NULL;
return;
}
void list_print(struct list_t *list)
{
struct list_t *p = NULL;
p = list;
while (p->next != NULL)
{
printf("%02d\n", p->data);
p = p->next;
if (p->next == NULL)
{
printf("%02d\n", p->data);
break;
}
}
}
void list_free(struct list_t *list)
{
struct list_t *p = NULL;
p = list;
while (p->next != NULL)
{
p = p->next;
free(p->next);
p->next = NULL;
if (p->next == NULL)
{
break;
}
}
}

20
src/list.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef _LIST_H_
#define _LIST_H_
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct list_t
{
uint8_t data;
struct list_t *next;
} list_t;
void list_init(struct list_t *list);
void list_add(struct list_t *list, uint8_t data);
void list_print(struct list_t *list);
void list_free(struct list_t *list);
#endif

View File

@ -1,8 +0,0 @@
#include "test.h"
#include <stdio.h>
void test()
{
printf("test\n");
}