https://ke.qq.com/course/466167?taid=4166582134054135
#include <stdio.h>
/*
* 执行命令 ./hello kodulf
* argc = 2
* argv[0] = ./hello
* argv[1] = kodulf
*
*/
int main(int argc, char **argv){
if(argc >= 2){
printf("hello world %s \n",argv[1]);
} else {
printf("hello world");
}
return 0;
}
执行:这里的-o 是output 的意思
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# gcc -o hello hello.c
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# ls
hello hello.c
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# ./hello kodulf
hello world kodulf
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1#
1: 头文件的作用,告诉别人,告诉编译器,这个函数应该怎么调用
2: 头文件在哪里?分为两种情况,
第一在默认路径里面,可以通过一下命令找到默认的,我们一般使用的是/usr/include 里面的那个
root@iZrj9e3565o1e423gw4xgiZ:/# find . -name "stdio.h"
./usr/include/stdio.h
./usr/include/x86_64-linux-gnu/bits/stdio.h
./usr/include/c++/7/tr1/stdio.h
第二在可以指定,
在代码里面制定的话就是,默认的使用<> ,如果是当前路径使用""引号
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# touch test.h
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# vim hello.c
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# gcc -o hello hello.c
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# vim hello.c
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# gcc -o hello hello.c
hello.c:2:10: fatal error: test.h: No such file or directory
#include <test.h>
^~~~~~~~
compilation terminated.
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# find / -name "stdio.h"
/usr/include/stdio.h
/usr/include/x86_64-linux-gnu/bits/stdio.h
/usr/include/c++/7/tr1/stdio.h
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# cp test.h /usr/include/
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# gcc -o hello hello.c
编译的时候制定,添加-I 的选项 I 是i的大写, 可以认为是include 的第一个字符的
#include <stdio.h>
#include <test.h>
#include <test2.h>
/*
* 执行命令 ./hello kodulf
* argc = 2
* argv[0] = ./hello
* argv[1] = kodulf
*
*/
int main(int argc, char **argv){
if(argc >= 2){
printf("hello world %s \n",argv[1]);
} else {
printf("hello world");
}
return 0;
}
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# touch /test/test2.h
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# vim hello.c
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# gcc -o hello hello.c
hello.c:3:10: fatal error: test2.h: No such file or directory
#include <test2.h>
^~~~~~~~~
compilation terminated.
root@iZrj9e3565o1e423gw4xgiZ:/linux_study/lesson_1# gcc -o hello hello.c -I /test
第三printf函数在哪里呢?
库的默认的路径是lib 目录
注意要区分 -I, -l 和-L 的区别, 后面讲解
-I (大写i)指定头文件目录
-l (小写l)制定某个库
-L 指定库目录
911



被折叠的 条评论
为什么被折叠?



