linux c 获取文件路径和文件名
linux 提供了两个函数分别用来获取文件所在的目录和文件名:
|
1 2 |
char *dirname(char *path); char *basename(char *path); |
它们被包含在头文件 libgen.h 中,dirname 取得的路径名不包含最后的/,basename 取自于最后一个下划线后的内容。 以下是几个示例: 路径 dirname ba ... 阅读更多
linux 提供了两个函数分别用来获取文件所在的目录和文件名:
|
1 2 |
char *dirname(char *path); char *basename(char *path); |
它们被包含在头文件 libgen.h 中,dirname 取得的路径名不包含最后的/,basename 取自于最后一个下划线后的内容。 以下是几个示例: 路径 dirname ba ... 阅读更多
一、原理 linux 支持多进程间共享打开文件,即同一时刻允许多个进程同时打开同个文件,每个进程之间的读写操作互不影响。 为了实现这一个机制,linux 内核使用了三种数据结构来表示打开的文件,它们之间的关系决定了在文件共享方面一个进程对另一个进程可能产生的影响。 1.1 内核数据结构 每个进程的进程表 ... 阅读更多
一、问题描述 在编译 C++程序时,遇到以下问题:
|
1 2 3 |
/tmp/cccZeFer.o: In function `main': main.cpp:(.text+0xf): undefined reference to `****' collect2: error: ld returned 1 exit status |
看到错误的第一直觉是共享库出问题了,因为以前出现这个问题都是因为库没有加进来,但是反复确认过后发现共享库并没有问题。 第一:编译的时候使用-l 选项包含了库文件,并且库里面的函数也存在。 ... 阅读更多
一、问题描述 C 语言中的函数提供了一种可变长参数机制,这个机制使得我们在操作的时候充分自定义自己的功能,例如使用最多的 printf 函数:
|
1 |
printf("%s: %d", "HelloWorld", 10); |
它的函数声明为:printf(const char *fmt, ...);,其中的... 就代表不 ... 阅读更多
一、概述 一个程序在从源码变成二进制程序之间一共有四个步骤:预处理,编译,生成目标文件,链接。 以下展示了一个简单的加法程序的编译过程:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// 文件 add.h int add(int i, int j); // 文件 add.cpp int add(int i, int j) { return i + j; } // 文件 main.cpp #include <iostream> #include "add.h" using namespace std; int main(){ // 这里是一行注释 cout << add(1, 2) << endl; return 0; } |
二、预处理 预处理阶段主要有以下的操作: 头文件替换 宏定义替换 删除注释 预处理指令处理 在 G ... 阅读更多
STL 中的 string 类有两个方法 size() 和 length() 用来返回字符串的长度。 两者在实现上没有区别:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
> sed -n 907,918p /usr/include/c++/7/bits/basic_string.h // Capacity: /// Returns the number of characters in the string, not including any /// null-termination. size_type size() const _GLIBCXX_NOEXCEPT { return _M_string_length; } /// Returns the number of characters in the string, not including any /// null-termination. size_type length() const _GLIBCXX_NOEXCEPT { return _M_string_length; } |
一、信号函数的理解 C 语言中信号函数的原型为:
|
1 |
void (*signal(int signo, void (*func)(int)))(int); |
这个函数定义看起来十分复杂,可以分为以下两步来理解: 首先看 signal(int signo, void (*func)(int)) 部分,signal 是一个函数,它的形参为一个 int 类型 ... 阅读更多
使用 exec 族函数时抛出以下警告:
|
1 2 3 4 |
exec.c: In function 『main』: exec.c:8:3: warning: missing sentinel in function call [-Wformat=] if (execlp("/bin/ls", "/bin/ls", "-l", ".") == -1) ^ |
错误的原因在 man page 中找到:
|
1 |
The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argu‐ment, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a null pointer. |
execv, execvp, execvpe 这几个函数可以给新创建的进程传递参数,但是 The ... 阅读更多
三者的区别: return 作用于函数,使用 return 只是退出当前函数,而 exit 和_exit 直接终止程序。 return 和 exit 在退出各自作用域前会自动刷新缓冲区,_exit 不会刷新当前缓冲区。 例如以下代码的 f 函数中使用 return,exit 和_exit 退出的结果都不一样。 [crayon-6 ... 阅读更多
一、静态库和动态库 静态库是指程序在编译阶段就把库文件嵌入到程序中的三方库,这种行为在程序运行前就已经决定了,程序在编译完成后不再依赖库文件。 动态库和静态库不一样,它是在程序运行期间才发生的调用行为,不会嵌入到程序,相对来说,链接动态库的二进制文件体积更小。 windows 和 linux 平台下的静态 ... 阅读更多