使用MyString

使用MyString

在使用我们写的函数时,首先要有头文件,方便其他函数调用,其中宏定义是防止重复调用头文件,如果想让C++程序使用我们的函数,可以加上#ifdef __cplusplus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _MYSTRING_H
#define _MYSTRING_H

#ifdef __cplusplus
extern "C" {
#endif
int my_strlen(const char *str);
int my_strncmp(const char *str1, const char *str2, int size);
int my_strcmp(const char *str1, const char *str2);
int startwith(const char *str1,const char *str2);
int endwith(const char *str1,const char *str2);
int my_strcpy(char *str1,const char *str2);
int my_strncpy(char *str1,const char *str2,int length);
int my_strcat(char *str1,const char *str2);
int my_strncat(char *str1,const char *str2,int length);
char *my_strstr(const char *str1, const char *str2);
char *my_strnstr(const char *str1, const char *str2, int length);
char *my_strchr(const char* str1, int c);
char *my_strrchr(const char* str1, int c);
char *my_strpbrk(const char *str1, const char *str2);
int my_strspn(const char *str1, const char *str2);

#ifdef __cplusplus
}
#endif

#endif

使用MyString的方式

将代码添加进项目

想要使用自己造的轮子最简单的方法就是直接将其放到要使用的项目中,在项目中添加头文件和源码

自己使用的话当然没问题,但是其他人想使用呢?我只想让他用,但是我不想让他直到我的轮子是怎么造的,这要怎么做呢?

提供编译好的库

我们直接给他成品(库文件),和说明书(头文件)。他只能按照说明书使用,但是不知道具体的制造过程。就像是我们写程序时第一行下意识的就会写一个#include <stdio.h> 这就是使用了标准库,我们一直在使用一些库,只是没有在意这件事而已

编译生成库

在这里使用CMake生成Makefile,然后编译安装到系统目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## 编译此项目所需cmake的最低版本
cmake_minimum_required(VERSION 3.5)
## 设置安装路径
set(CMAKE_INSTALL_PREFIX /usr)
## 设置项目名称,版本,编程语言
project(mystrlib VERSION 0.0.1 LANGUAGES C)
## 设置编译目标的类型及源码
add_library(${PROJECT_NAME} SHARED
mystring.c
)
## 设置编译目标的属性
set_target_properties(${PROJECT_NAME} PROPERTIES
SOVERSION 0.0.1
PUBLIC_HEADER "mystring.h"
)
## 设置安装路径
install(TARGETS mystrlib
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include/mystr
)

将上述内容存进CMakeList.txt,并和代码同级,执行一下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ mkdir build
$ cd build
$ cmake ..
$ make && make install

输出:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/carl/test/startwith/build
[ 50%] Building C object CMakeFiles/mystrlib.dir/mystring.c.o
[100%] Linking C shared library libmystrlib.so
[100%] Built target mystrlib

Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/libmystrlib.so.0.0.1
-- Installing: /usr/lib/libmystrlib.so
-- Installing: /usr/include/mystr/mystring.h

到此,我们已经成功的编译出来了库文件,并安装到了系统目录中,现在就可以使用它了

使用编译好的库文件

简单的测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <mystr/mystring.h>

int main()
{
char str1[] = "hello world!";
char str2[] = "h";
char str3[] = "!";

int ret = startwith (str1, str2);
printf ("ret = %d\n", ret);
ret = endwith (str1, str3);
printf ("ret = %d\n", ret);

}

使用gcc编译:

1
2
3
4
5
6
7
8
9
$ gcc test.c
报错:
/tmp/ccAAlrV0.o: In function 'main':
test.c:(.text+0x4b): undefined reference to 'startwith'
test.c:(.text+0x75): undefined reference to 'endwith'
collect2: error: ld returned 1 exit status

$ gcc test.c -lmystrlib
别忘了链接上我们的库才能编译成功

编译出来以后执行一下,看看结果:

1
2
3
$./a.out
ret = 0
ret = 0

问题

写到这里我愈发觉得自己的Makefile和CMake底子太弱,需要好好学习一下同时需要对编译原理进行深入的学习,库和可执行文件的区别在哪里,它在编译的第几阶段,如果程序出了问题怎么找到问题原因并解决?

问题太多需要一个一个解决,下一篇,我会先学习并整理CMake相关的知识,后面会学习编译与链接的理论知识并分享出来

愈发觉得这个系列的没有章法,想到哪写到哪,惭愧惭愧


使用MyString
https://carl-5535.github.io/2021/07/28/造轮子/使用MyString/
作者
Carl Chen
发布于
2021年7月28日
许可协议