关于C语言多个源文件编译成一个程序

    技术2022-05-19  20

    第一次用的博客,慕名而来,决定好好经营下自己的技术博客。

    今天看《C和指针》中第二章编程训练的第一题,就是关于C语言多个源文件编译成一个程序的问题。之前自己一直都不太搞得懂这个,不过之前写的程序都是简单的,一个源文件就能搞定的那种,所以也一直懒了没有去学怎么,心里隐隐的对学习心的东西感觉到恐惧。今天自己搬弄了一个下午,各种错误,要崩溃了。吃完饭,又继续投入到这其中当去。这次学聪明了,Google了下,然后学着对比着来修改自己的代码。

    这里就举自己写的一个很简单的范例来解说。

     

    题目是,一共有三个源文件,第一个是increment,实现将整数加一;第二个是negate,实现将整数变反;最后的是main函数,实现调用这两个函数,要求这三个函数在不同的源文件。

    实现起来是这样的——

     

    首先编写一个 increment.h的头文件,申明函数

    extern int increment(int a);

     

    再写increment.c源文件

    #include“increment.h”

    int increment(int a)

    {

           return a+1;

    }

    再将该源文件编译

     

    同理编写negate.h&negate.c

     

    最后来写main.c

    #include"increment.h"#include"negate.h"#include"stdio.h"

    int main(){ int a=10; printf("%d/n",increment(a)); printf("%d/n",negate(a)); return 0;}

    编译&执行,但是比较崩溃的是一直出现一个错误——

    LIBCD.lib(wincrt0.obj)   :   error   LNK2001:   unresolved   external   symbol   _WinMain@16

    事实上我不懂什么意思,不过Google比较方便,就Google一下这个错误,出来一些处理方法,选了一个很简单的用了

    project-> setings-> link-> project   Option编辑筐中把/subsystem:windows 改为/subsystem:console

    改了之后果然能够运行了。

    高兴中~~~

    不过原理还是不懂。

     

    以下引用——

    SYMPTOMS If   you   try   to   build   a   release   mode   of   an   ATL   service   EXE   created   with   ATL   COM   AppWizard,   you   get   the   following   the   error   message:   "error   LNK2001:   unresolved   external   symbol   _main "   CAUSE Builds   in   release   mode   automatically   include   the   preprocessor   directive   _ATL_MIN_CRT,   while   the   default   ATL   service   code   generated   by   the   wizard   requires   the   CRT   library.   RESOLUTION Remove   _ATL_MIN_CRT   from   the   list   the   preprocessor   defines   to   allow   CRT   startup   code   to   be   included.  


    最新回复(0)