LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1981|回复: 3

linux下对C语言文件进行批量编译

[复制链接]
发表于 2008-5-23 12:49:34 | 显示全部楼层 |阅读模式
pid=fork();
if(pid<0) printf("fork error!\n");
else if(pid==0)
{if(execle("/usr/bin/gcc","gcc","./my.c","-o","./myt.out",(char *)0,env_init)<0)
printf("exec error!\n");
exit(0);这其中有一句代码{if(execle("/usr/bin/gcc","gcc","./my.c","-o","./myt.out",(char *)0,env_init)<0)是对my.c进行编译,生成myt.out,我现在想实现批量编译一个指定文件夹下的所有文件,生成的文件也统一存放在指定文件夹中,要怎么改程序,才能实现这以功能呢?
发表于 2008-5-23 15:28:21 | 显示全部楼层
批量编译源文件?你先研究下Makefile吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-5-26 12:43:05 | 显示全部楼层
我写了这么一个程序:
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
        DIR        *dp;
        struct dirent        *dirp;
        char name[100][100];
        int i=0;
        if (NULL == (dp=opendir(argv[1])))
        {
       fprintf(stderr, "Can' open %s directiory\n",argv[1], strerror(errno));
                exit(-1);
        }
        while ( NULL != (dirp = readdir(dp)))
        {
        const char *p = dirp->d_name;
        for (; *p && *p != '.'; ++p);
        if (0 == strcmp(p, ".c"))
                {
                        printf("File name: %s\n", dirp->d_name);
                strcpy(name[i++], dirp->d_name);
                }
        }
        closedir(dp);
return 0;
}
他实现了 打开指定路径下的所有.c文件,之后就是对其中的.c文件进行逐个编译了,我就是卡在这里,不知道该怎么实现逐个编译,请高手指教啊 !
回复 支持 反对

使用道具 举报

发表于 2008-6-5 13:27:33 | 显示全部楼层

帮你写好了,还有问题么?

#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    DIR *dp;
    pid_t pid;
    int status=0;
    struct dirent *dirp;
    char name[100][100];
    char buf[80];
    int i=0;
    int file_num, result;

    char* cmd_argv[] = {"gcc","-o","exec_file","source_file",(char *)0};
    const char exec_file_type[] = ".out";

    if (NULL == (dp=opendir(argv[1])))
    {
        fprintf(stderr, "Can' open %s directiory\n",argv[1], strerror(errno));
        exit(-1);
    }

    while ( NULL != (dirp = readdir(dp)))
    {
        const char *p = dirp->d_name;
        for (; *p && *p != '.'; ++p);

        if (0 == strcmp(p, ".c"))
        {
            //printf("File name: %s\n", dirp->d_name);
            strcpy(name[i++], dirp->d_name);
        }
    }
    file_num = i;
    closedir(dp);

    // enter into working directory
    chdir(argv[1]);
    getcwd(buf, sizeof(buf));
    printf("Current working directory is %s\n", buf);
    printf("total %d source files\n\n", file_num);

    // Begin to work      
    for (i=0; i<file_num; i++)
    {
        printf("start compile %s\n", name);
        // set output filename
        int len = strlen(name) + sizeof(exec_file_type) + 1;
        char* temp = malloc( len * sizeof(char));
        strcpy(temp, name);
        cmd_argv[2] = strcat(temp, exec_file_type);
        // set input filename
        cmd_argv[3] = name;

        pid = fork();
        if (pid < 0)
        {
            printf("fork failed, try again\n");
            exit(1);
        } else if (pid == 0)
            {
                // This is child process
                //printf("This is child process : %d\n", getpid());
                if ( (result=execv("/usr/bin/gcc", cmd_argv)) == -1 )  
                {
                    printf("compile %s failed\n", name);                                                            
                    printf("error: %d\n", errno);            
                }
            
            } else {
                // This is parent process, wait for child process finish.
                pid = wait(&status);
                //printf("child's pid = %d\texit status=%d\n",pid,status);
                printf("finish compile %s\n\n", name);
                if(temp != NULL) free(temp);
            }
    }

    return 0;
}

结果
e390977@ch71DT4nwhz1x /home/ray/c++/make
$gcc -o my_make my_make.c

e390977@ch71DT4nwhz1x /home/ray/c++/make
$./my_make.exe  test/
Current working directory is /home/ray/c++/make/test
total 2 source files

start compile calnumber.c
finish compile calnumber.c

start compile my_make.c
finish compile my_make.c


e390977@ch71DT4nwhz1x /home/ray/c++/make
$ll test/
total 28K
-rw-r--r-- 1 e390977 ????????     291 May 26 13:58 calnumber.c
-rwxr-xr-x 1 e390977 mkgroup-l-d 9.2K Jun  5 13:16 calnumber.c.out*
-rw-r--r-- 1 e390977 mkgroup-l-d 2.6K Jun  5 13:10 my_make.c
-rwxr-xr-x 1 e390977 mkgroup-l-d  14K Jun  5 13:16 my_make.c.out*

e390977@ch71DT4nwhz1x /home/ray/c++/make
$file test/*
test/calnumber.c:     ASCII C program text
test/calnumber.c.out: MS-DOS executable PE  for MS Windows (console) Intel 80386 32-bit
test/my_make.c:       ASCII C program text, with CRLF line terminators
test/my_make.c.out:   MS-DOS executable PE  for MS Windows (console) Intel 80386 32-bit

头一次贴代码,居然不能缩进。到我的博客上看吧http://raychen1984.cublog.cn/
回复 支持 反对

使用道具 举报

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

本版积分规则

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