编译链接不同文件夹的c代码和头文件

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

void getname( char *name , char *tmp )
{
	char tail[10];
	int i = 0 , j = 0;
	
	while(  name[ i++ ] != '\\' )
		;
	while( name[ i ] )
		tail[ j++ ] = name[ i++ ];
	tail[ --j ] = 'o';
	tail[ ++j ] = '\0';
	strcat( tmp , tail );
}

//example的字符形如 list\ex-1.c ,其中list是文件夹的名字
//目的是得到字符集“gcc -g -c E:\AlgoritbmsCode\examples_pc\examples\list\ex-1.c -IE:\AlgoritbmsCode\examples_pc\include -o E:\AlgoritbmsCode\examples_pc\binary\ex-1.o”
//并把exam_o用obj文件名写好,例如ex-1.o,以便后面的link程序使用

void compile_exam( char *example , char *exam_o )
{	
	int i = 0 , j = 0;
	char MyCommand[200] = "gcc -g -c E:\\AlgoritbmsCode\\examples_pc\\examples\\";
	char tmp[100] = " -IE:\\AlgoritbmsCode\\examples_pc\\include -o E:\\AlgoritbmsCode\\examples_pc\\binary\\";           //81个字符
	
	strcat( MyCommand , example );	
	getname( example , tmp );          //把example指向的字符集后一个文件名提取出来,并把它的最后一个字符也就是c改成o,然后添加到tmp的尾部
	strcat( MyCommand , tmp );
	//printf("\n%s\n",MyCommand);
	system( MyCommand );
	
	i = strlen(MyCommand);
	while( MyCommand[ i-- ] != '\\' )
		;
	//从上面循环出来的i指向的是最后的两个'\\'中的前面一个,所以接下来的一行使用的是++i,这样指向的就是后面的一个‘\’,但是还是没有指向我们所需要的字符,所以在下面的赋值中我们使用的是++i
	++i;          
	while( MyCommand[ i ] )
		exam_o[ j++ ] = MyCommand[ ++i ];
		
}

void compile_src(  char *source , char *src_o )
{
	int i = 0;
	int j = 0;
	char MyCommand[200] = "gcc -g -c E:\\AlgoritbmsCode\\examples_pc\\source\\";
	char tmp[100] = " -IE:\\AlgoritbmsCode\\examples_pc\\include -o E:\\AlgoritbmsCode\\examples_pc\\binary\\";           //81个字符
	
	strcat( MyCommand , source );
	strcat( tmp , source );
	strcat( MyCommand , tmp );
	MyCommand[ strlen( MyCommand ) - 1 ] = 'o';
	
	//printf("\n%s\n",MyCommand);
	system( MyCommand );
	
	i = strlen(MyCommand);
	while( MyCommand[ i-- ] != '\\' )
		;
	++i;
	while( MyCommand[ i ] )
		src_o[ j++ ] = MyCommand[ ++i ];	 
}

void link( char *exam_o , char *src_o )
{
	char MyCommand[200] = "gcc -g ";
	int i = 0;
	char binary_file_name[10];
	char path[50] = "E:\\AlgoritbmsCode\\examples_pc\\binary\\";
	
	while( src_o[i] )
		binary_file_name[i++] = src_o[i];
	--i;
	
	//得到扩展名为exe的名字
	binary_file_name[ i++ ] = 'e';
	binary_file_name[ i++ ] = 'x';
	binary_file_name[ i++ ] = 'e';
	binary_file_name[ i ] = '\0';
	
	//将命令组合起来,使之成为"gcc -g E:\\AlgoritbmsCode\\examples_pc\\binary\\ex-1.o E:\\AlgoritbmsCode\\examples_pc\\binary\\list.o -o E:\\AlgoritbmsCode\\examples_pc\\binary\\list.exe"
	strcat( MyCommand , path );
	strcat( MyCommand , exam_o );
	strcat( MyCommand , " " );
	strcat( MyCommand , path );
	strcat( MyCommand , src_o );
	strcat( MyCommand , " -o " );
	strcat( MyCommand , path );
	strcat( MyCommand , binary_file_name );
	//printf( "%s\n",MyCommand );
	
	system( MyCommand );
}

int main( int argc , char *argv[] )
{
	char exam_o[10] , src_o[10];
	
	compile_exam( argv[1] , exam_o );
	compile_src( argv[2] , src_o );
	
	link( exam_o , src_o );
	return 1;
}


转载于:https://my.oschina.net/nibnat/blog/191864

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 GCC 编译多文件程序和头文件涉及以下几个步骤: 1. **组织文件结构**: - 将相关的源代码文件 (.c) 分别放在各自的文件夹中,比如有一个 `main.cpp`, `header1.h`, 和 `header2.h`。 - 将公共头文件(通常包含了函数声明或宏定义)放在一起,例如在 `include` 文件夹下。 2. **头文件包含**: - 在每个源文件中(除了那些不需要头文件的),使用 `#include` 指定对应的头文件,例如 `#include "header1.h"`。 3. **编译源文件**: - 对于每个源文件,单独编译它。假设你有三个源文件,分别编译为三个.o 文件: - `gcc main.cpp -c -o main.o` - `gcc header1.cpp -c -o header1.o` - `gcc header2.cpp -c -o header2.o` 4. **链接生成可执行文件**: - 使用 `gcc` 连接所有 .o 文件并生成可执行文件: - 如果你的 `main.cpp` 包含了其他.o 文件,那么: ``` gcc main.o header1.o header2.o -o your_program ``` `-o` 参数指定了最终的可执行文件名。 5. **使用预处理指令**: - 如果头文件包含条件编译或者其他预处理指令,你可能需要使用 `-E` 或 `-P` 选项来编译仅提取预处理信息。 6. **添加包含路径**: - 如果头文件不是在当前目录下,可以使用 `-I` 选项添加包含路径,如 `-I include`。 **注意事项**: - 保证所有的源文件都被正确的编译并且链接到了一起,特别是当有循环依赖时需要特别注意。 - 如果你在编写 C++ 项目,还需要考虑链接库 (-l flag) 和链接器选项 (-L flag),特别是动态链接库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值