佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 2092|回复: 25

C 的问题

[复制链接]
发表于 2-11-2009 11:03 PM | 显示全部楼层 |阅读模式
请问在C里什么是Marco, Make File 和system call 。我看了google 但是也不明白,可以给些example吗?
回复

使用道具 举报


ADVERTISEMENT

发表于 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 大大说的, 是一个巨集指令, 也就是说, 集合很多步骤在一起, 由一个符号,标记来代表起指令。

举个例子
  1. #include <stdio.h>

  2. #define COUNT 1 + 1

  3. int main()
  4. {
  5.     printf("%d\n", COUNT);

  6.     return 0;
  7. }
复制代码
这里, macro COUNT, 代表了 1 + 1,

因此在 printf COUNT, 你将会得到 2

在举个例子
  1. #include <stdio.h>
  2. #define PRINT(x,y) \
  3. { \
  4.     printf("value of x = %s\n", x); \
  5.     printf("value of y = %s\n", y); \
  6. }

  7. int main()
  8. {
  9.     PRINT("abc" , "def";

  10.     return 0;
  11. }
复制代码
这里, 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 编辑 ]
回复

使用道具 举报

Follow Us
 楼主| 发表于 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
  1. macro: macro.cpp
  2. <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:
  1. yee@zyee:~/programming/make$ make
  2. g++ macro.cpp -o macro
  3. yee@zyee:~/programming/make$ ./macro
  4. abc
  5. def
  6. yee@zyee:~/programming/make$
复制代码
在来一个 example
  1. all: macro
  2.         
  3. macro: macro.o
  4.         g++ macro.o -o macro

  5. macro.o: macro.cpp
  6.         g++ -c macro.cpp
复制代码
这个于之前的是一样的, 不同的是, 我将之区分为更多的 dependency, 好让你明白

output:
  1. yee@zyee:~/programming/make$ make
  2. g++ -c macro.cpp
  3. g++ macro.o -o macro
  4. yee@zyee:~/programming/make$ ./macro
  5. abc
  6. def
  7. yee@zyee:~/programming/make$
复制代码
makefile 里面 还可以加入 variable, 好比, 设定 output name, 活设定compiler 名字等, 如
  1. OUTPUT_FILE_NAME = program

  2. all: macro
  3.         
  4. macro: macro.o
  5.         g++ macro.o -o ${OUTPUT_FILE_NAME}

  6. macro.o: macro.cpp
  7.         g++ -c macro.cpp
复制代码
或是
  1. COMPILER = g++

  2. all: macro
  3.         
  4. macro: macro.o
  5.         ${COMPILER} macro.o -o macro

  6. macro.o: macro.cpp
  7.         ${COMPILER} -c macro.cpp
复制代码
甚至可加入 clean, 来清除object file

如下列
  1. all: macro
  2.         
  3. macro: macro.o
  4.         g++ macro.o -o macro

  5. macro.o: macro.cpp
  6.         g++ -c macro.cpp

  7. clean:
  8.         rm macro.o
复制代码
OUTPUT:
  1. yee@zyee:~/programming/make$ make
  2. g++ -c macro.cpp
  3. g++ macro.o -o macro

  4. yee@zyee:~/programming/make$ ls -al
  5. total 40
  6. drwxr-xr-x  2 yee yee 4096 2009-11-03 18:40 .
  7. drwxrwxrwx 17 yee yee 4096 2009-11-03 18:16 ..
  8. -rwxr-xr-x  1 yee yee 8946 2009-11-03 18:40 macro
  9. -rw-r--r--  1 yee yee  175 2009-11-03 00:20 macro.cpp
  10. -rw-r--r--  1 yee yee 1968 2009-11-03 18:40 macro.o
  11. -rw-r--r--  1 yee yee  109 2009-11-03 18:39 Makefile

  12. yee@zyee:~/programming/make$ make clean
  13. rm macro.o

  14. yee@zyee:~/programming/make$ ls -al
  15. total 36
  16. drwxr-xr-x  2 yee yee 4096 2009-11-03 18:40 .
  17. drwxrwxrwx 17 yee yee 4096 2009-11-03 18:16 ..
  18. -rwxr-xr-x  1 yee yee 8946 2009-11-03 18:40 macro
  19. -rw-r--r--  1 yee yee  175 2009-11-03 00:20 macro.cpp
  20. -rw-r--r--  1 yee yee  109 2009-11-03 18:39 Makefile
复制代码
执行 make clean 后, macro.o 就被清除了

[ 本帖最后由 onlylonly 于 3-11-2009 06:53 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 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 | 显示全部楼层

回复 9# 上帝会保佑我 的帖子

小子依然是学生一名。
回复

使用道具 举报

发表于 8-11-2009 05:22 PM | 显示全部楼层

回复 10# onlylonly 的帖子

三人行,必有我师焉。择其善者而从之,其不善者而改之.
大家永远都是学生
回复

使用道具 举报

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

回复 10# onlylonly 的帖子

你真的是好厉害。小弟虽然毕业不久也得佩服你。
回复

使用道具 举报

发表于 9-11-2009 09:08 PM | 显示全部楼层
我想说。。。找个ide不就成了么。。。
回复

使用道具 举报

 楼主| 发表于 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
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <sys/types.h>

  4. using namespace std;

  5. int main()
  6. {
  7.     cout << "program started" << endl << endl;

  8.     cout << "parent process pid = " << getpid() << endl << endl;

  9.     pid_t returnValue = fork();

  10.     cout << "fork() return " << returnValue << endl;

  11.     if(returnValue > 0)
  12.         cout << "This is parent process, child pid = "<< returnValue << endl << endl;
  13.     else if( returnValue == 0 )
  14.         cout << "This is child process" << endl << endl;
  15.     else
  16.         cout << "error creating process" << endl << endl;



  17.     return 0;
  18. }
复制代码
OUTPUT:
  1. program started

  2. parent process pid = 12917

  3. fork() return 12918
  4. This is parent process, child pid = 12918

  5. fork() return 0
  6. 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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 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# 上帝会保佑我 的帖子

其实我也没什么厉害的, 懂得就是你看到这么多, 其余的, 都还是一知半解。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 2-12-2025 02:54 PM , Processed in 0.147262 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表