|
|
|
请问在C里什么是Marco, Make File 和system call 。我看了google 但是也不明白,可以给些example吗? |
|
|
|
|
|
|
|
|
|
|
发表于 2-11-2009 11:35 PM
|
显示全部楼层
Macro(巨集指令)简单来说就是用一个标记来代表一系列的指令,然后只要呼叫那个标记,就会执行其所代表的指令集。
Make File 可做两种解释:1)作为动词,Make File就是编译你的源代码,将它转换成机器码,2)作为名词,就是Make程序所产生的档案。
System Call 顾名思义,我就懒得说了。
你真的有google过吗?Wikipedia里面都有很详细的解释,通常google得来的结果的头几名都会出现Wikipedia的连接的。 |
|
|
|
|
|
|
|
|
|
|
发表于 3-11-2009 12:33 AM
|
显示全部楼层
1. macro 就如 geekman 大大说的, 是一个巨集指令, 也就是说, 集合很多步骤在一起, 由一个符号,标记来代表起指令。
举个例子- #include <stdio.h>
- #define COUNT 1 + 1
- int main()
- {
- printf("%d\n", COUNT);
- return 0;
- }
复制代码 这里, macro COUNT, 代表了 1 + 1,
因此在 printf COUNT, 你将会得到 2
在举个例子- #include <stdio.h>
- #define PRINT(x,y) \
- { \
- printf("value of x = %s\n", x); \
- printf("value of y = %s\n", y); \
- }
- int main()
- {
- PRINT("abc" , "def";
- return 0;
- }
复制代码 这里, PRINT, 代表了
{
printf("value of x = %s\n", x);
printf("value of y = %s\n", y);
}
, 因此在 main 里呼叫 PRINT, 就会执行其代表的指令。
* c 里, macro 只能以一行来代表, 因此我在每行最尾端加入了 \ ,来代表此指令还未完, 接下来一行依然是指令。
[ 本帖最后由 onlylonly 于 3-11-2009 12:43 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 3-11-2009 12:40 AM
|
显示全部楼层
2. make file 普遍上是 geekman 大大说的第二向, 也就是由 make 执行的一系列指令。
一般上在 *nix 系统力, 如 linux, bsd, solaris 等, 用户都习惯自行编译软件。但是在编译过程上, 不是一般的 gcc -wall xxx 就行了。 好比说, 必须先编译每一个 object file, 过后在以 linker link, 最后方便用户, 还得清除编译出来的 object file。 可能过程中, 程式员还希望加入其他的指令等。
因此为了方便用户, 以及日后的编译工作, 我们一般会将说有指令写入一个叫 makefile 的文件, 如编译之前干什么, 过后干什么, 编译比去执行什么指令, 全部写入这个makefile 文件。而往后, 凡是要编译该软件, 久无须麻麻烦烦的将指令个别输入。 直接make就好了 |
|
|
|
|
|
|
|
|
|
|
发表于 3-11-2009 12:55 AM
|
显示全部楼层
system call 是 OS 提供的服务的接口, 比如你要open file, 你久可能呼叫 open(), 来向 OS 请求 open file。 OS 接受你的 system call, 执行完毕后, 久将会 return 结果给你。
一般上很少人会直接运行 system call。 而是选择一些已经存在的 library , 如选择 stdio 的 printf, 而不是 system call 的 write() , 一来是 privillage, 而来是 system call 很危险的, 滥用的话你的 system 应该不保了, 第三, library 已经整合了许多我们日常需要用到的function, 而这个function, 已经包含了必须要的 system call。 我们就无需个别执行, 直接呼叫 API 就好 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 3-11-2009 01:39 PM
|
显示全部楼层
原帖由 geekman 于 2-11-2009 11:35 PM 发表 
Macro(巨集指令)简单来说就是用一个标记来代表一系列的指令,然后只要呼叫那个标记,就会执行其所代表的指令集。
Make File 可做两种解释:1)作为动词,Make File就是编译你的源代码,将它转换成机器码,2)作 ...
Got. I knew what is macro and system call already but the make file I don't know how to create. Sorry my company don't have the tools for write Chinese word so reply using English
[ 本帖最后由 上帝会保佑我 于 3-11-2009 01:41 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 3-11-2009 01:49 PM
|
显示全部楼层
原帖由 onlylonly 于 3-11-2009 12:55 AM 发表 
system call 是 OS 提供的服务的接口, 比如你要open file, 你久可能呼叫 open(), 来向 OS 请求 open file。 OS 接受你的 system call, 执行完毕后, 久将会 return 结果给你。
一般上很少人会直接运行 system ...
About the make file. The purpose for the make file is it put all the .c file together and compile once only which is no need to compile one by one. Do you know how to create a Make File? |
|
|
|
|
|
|
|
|
|
|
发表于 3-11-2009 06:45 PM
|
显示全部楼层
回复 7# 上帝会保佑我 的帖子
makefile 可以很简单, 也可以很复杂。 甚至还可以包裹某些 shall script。
其中也可以个别 compile 不同 object file, 设定如何 compile object file 等。
这里只能介绍一些简单的 makefile syntax, 什么是 dependency 等。
p/s: make 不只是限制在 c / c++ 里, 其实任何 language 都可以用 make 来编译的。
makefile- macro: macro.cpp
- <tab> g++ macro.cpp -o macro
复制代码macro: macro.cpp
label dependency
label/filename 是用来定义如何, 以及需要什么来编译此文件。 在这里, 要编译 macro, 需要 dependency: macro.cpp 此文件。
<tab> g++ macro.cpp -o macro
重要: 《tab》 是必备的, 一定要。 这个是让 make 来区分 dependency 于 procedure。万万不可用 <space> 来代替
这里你看到, 要编译 macro, 久必须执行 g++ macro.cpp -o macro 此命令。
output:- yee@zyee:~/programming/make$ make
- g++ macro.cpp -o macro
- yee@zyee:~/programming/make$ ./macro
- abc
- def
- yee@zyee:~/programming/make$
复制代码 在来一个 example- all: macro
-
- macro: macro.o
- g++ macro.o -o macro
- macro.o: macro.cpp
- g++ -c macro.cpp
复制代码 这个于之前的是一样的, 不同的是, 我将之区分为更多的 dependency, 好让你明白
output:- yee@zyee:~/programming/make$ make
- g++ -c macro.cpp
- g++ macro.o -o macro
- yee@zyee:~/programming/make$ ./macro
- abc
- def
- yee@zyee:~/programming/make$
复制代码 makefile 里面 还可以加入 variable, 好比, 设定 output name, 活设定compiler 名字等, 如- OUTPUT_FILE_NAME = program
- all: macro
-
- macro: macro.o
- g++ macro.o -o ${OUTPUT_FILE_NAME}
- macro.o: macro.cpp
- g++ -c macro.cpp
复制代码 或是- COMPILER = g++
- all: macro
-
- macro: macro.o
- ${COMPILER} macro.o -o macro
- macro.o: macro.cpp
- ${COMPILER} -c macro.cpp
复制代码 甚至可加入 clean, 来清除object file
如下列- all: macro
-
- macro: macro.o
- g++ macro.o -o macro
- macro.o: macro.cpp
- g++ -c macro.cpp
- clean:
- rm macro.o
复制代码 OUTPUT:- yee@zyee:~/programming/make$ make
- g++ -c macro.cpp
- g++ macro.o -o macro
- yee@zyee:~/programming/make$ ls -al
- total 40
- drwxr-xr-x 2 yee yee 4096 2009-11-03 18:40 .
- drwxrwxrwx 17 yee yee 4096 2009-11-03 18:16 ..
- -rwxr-xr-x 1 yee yee 8946 2009-11-03 18:40 macro
- -rw-r--r-- 1 yee yee 175 2009-11-03 00:20 macro.cpp
- -rw-r--r-- 1 yee yee 1968 2009-11-03 18:40 macro.o
- -rw-r--r-- 1 yee yee 109 2009-11-03 18:39 Makefile
- yee@zyee:~/programming/make$ make clean
- rm macro.o
- yee@zyee:~/programming/make$ ls -al
- total 36
- drwxr-xr-x 2 yee yee 4096 2009-11-03 18:40 .
- drwxrwxrwx 17 yee yee 4096 2009-11-03 18:16 ..
- -rwxr-xr-x 1 yee yee 8946 2009-11-03 18:40 macro
- -rw-r--r-- 1 yee yee 175 2009-11-03 00:20 macro.cpp
- -rw-r--r-- 1 yee yee 109 2009-11-03 18:39 Makefile
复制代码 执行 make clean 后, macro.o 就被清除了
[ 本帖最后由 onlylonly 于 3-11-2009 06:53 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-11-2009 08:34 AM
|
显示全部楼层
回复 8# onlylonly 的帖子
|
Thank for the info. By the way, may i know your current job is it related to C and C++? |
|
|
|
|
|
|
|
|
|
|
发表于 5-11-2009 10:18 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 8-11-2009 05:22 PM
|
显示全部楼层
回复 10# onlylonly 的帖子
三人行,必有我师焉。择其善者而从之,其不善者而改之.
大家永远都是学生 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-11-2009 10:07 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 9-11-2009 09:08 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-11-2009 09:15 PM
|
显示全部楼层
原帖由 yeenfei 于 8-11-2009 05:22 PM 发表 
三人行,必有我师焉。择其善者而从之,其不善者而改之.
大家永远都是学生
哈哈。小弟不是学生已有一年了。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-11-2009 09:19 PM
|
显示全部楼层
回复 8# onlylonly 的帖子
|
想请问下你对 fork() 这个system call 熟不熟悉呢? 这个system call 是关于parent 和child (两个program) 可以run in parallel 是不是? |
|
|
|
|
|
|
|
|
|
|
发表于 10-11-2009 10:34 PM
|
显示全部楼层
fork() 其实是用来 create 一个与自己一样的 child process。
新建立的 child process 将从 fork() 的那行开始运行。
呼唤了fork()后, fork()会 return 一些 value。
pid_t returnValue = fork()
若 returnValue > 0, 这是 parent process, return 的是 child 的 pid
若 returnValue ==0, 这是 child process,
若 returnValue < 0, 说明 process creation fail- #include <iostream>
- #include <unistd.h>
- #include <sys/types.h>
- using namespace std;
- int main()
- {
- cout << "program started" << endl << endl;
- cout << "parent process pid = " << getpid() << endl << endl;
- pid_t returnValue = fork();
- cout << "fork() return " << returnValue << endl;
- if(returnValue > 0)
- cout << "This is parent process, child pid = "<< returnValue << endl << endl;
- else if( returnValue == 0 )
- cout << "This is child process" << endl << endl;
- else
- cout << "error creating process" << endl << endl;
- return 0;
- }
复制代码 OUTPUT:- program started
- parent process pid = 12917
- fork() return 12918
- This is parent process, child pid = 12918
- fork() return 0
- This is child process
复制代码 一般上, fork()会与 exec() 一起使用, 让 child process 执行 exec(), 成为新的程式。 ( 不知道怎么样解释, 不过基本上是 child process 执行 exec(), 然后 child process 的 code 会被 replace 掉, 成为新的 program code, 然后久不会回来了 )
[ 本帖最后由 onlylonly 于 10-11-2009 10:43 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 11-11-2009 09:45 PM
|
显示全部楼层
原帖由 上帝会保佑我 于 10-11-2009 09:15 PM 发表 
哈哈。小弟不是学生已有一年了。
当你知道越多的时候,就觉得懂得越少。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-11-2009 10:06 PM
|
显示全部楼层
原帖由 yeenfei 于 11-11-2009 09:45 PM 发表 
当你知道越多的时候,就觉得懂得越少。
此话何解? |
|
|
|
|
|
|
|
|
|
|
发表于 11-11-2009 10:40 PM
|
显示全部楼层
回复 18# 上帝会保佑我 的帖子
就是,当你懂得越多时, 同时会觉得还有太多太多的东西是你不懂的, 说一就觉得懂得其实自己什么也不懂。
这个世界真的很大, 比我强的高手大有所在。 就只是佳礼的 Linux 区, 高手不计其数了。 我在那里当真是连屎都不如。 个人力量真的很渺小 |
|
|
|
|
|
|
|
|
|
|
发表于 11-11-2009 10:41 PM
|
显示全部楼层
回复 12# 上帝会保佑我 的帖子
|
其实我也没什么厉害的, 懂得就是你看到这么多, 其余的, 都还是一知半解。。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|