如何在CMD下编译连接.CPP文件

本文介绍了如何在DOS命令行环境下,通过CL编译器和LINK连接器,将.CPP源文件编译并链接成可执行文件。以mycopy.cpp为例,详细步骤包括:先切换到源文件所在目录,使用CL编译生成.obj文件,再使用LINK进行连接生成.exe文件,最后运行编译后的程序实现文件复制功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何在CMD下编译连接.CPP文件  


/////////////////////////////////////////////mycopy.cpp存放在D盘下D:\mycopy.cpp(或是其它路径).

////////程序启动时的"命令行参数"与调用main()的"函数实参"不同.

////////命令行参数是由启动程序截获并找包成字符串数组后传递给main()的一个形参argValue的.

///////而包括命令字(即可执行文件名称)在内的所有参数的个数则被传递给形参argCount.

//mycopy.cpp
#include<stdio.h>
#include<iostream.h>

int main(int argCount, char* argValue[])
{
        FILE *srcFile = 0,*destFile=0;
        int ch = 0;
        if(argCount != 3)
       {
              cout<<argValue[0]<<endl;
              printf("Usage:%s src-file-name dest-file-name\n", argValue[0]);
        }
        else
       {
              if( (srcFile = fopen(argValue[1], "r")) == 0)
             {
                      printf("can not open source file\"%s\" !",argValue[1]);
              }
              else
             {
                      if((destFile = fopen(argValue[2], "w")) == 0)
                      {
                               printf("can not open destination file\"%s\"!",argValue[2]);
                               fclose(srcFile);
                       }
                       else
                       {
                              while((ch = fgetc(srcFile)) != EOF )
                                      fputc(ch, destFile);
                               printf("successful to copy a file!\n");
                               fclose(srcFile);
                               fclose(destFile);
                               return 0;
                        }
              }
       }
        return 1;
}

/////////////////////////////////////////////

 

然后DOS下切换到mycopy.cpp存放的盘符下,现我存放在D盘下

c:\Documents and Settings\Administrator>d:

D:\>CL mycopy.cpp               注释:编译mycopy.cpp , 产生mycopy.obj中间代码文件

D:\>LINK mycopy.obj                注释:连接mycopy.obj文件,生成mycopy.ext文件

D:\>mycopy d:\file2.txt   d:\myfile1.txt              注释:把file2.txt中的内容制到file1.txt文件中去


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值