How to compile C source code without a main function?

前端 未结 5 437
-上瘾入骨i
-上瘾入骨i 2020-12-13 02:25

How can I compile my C source files without needing to put a main function within them?

I get an error for the .cfiles that have no main fu

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 02:46

    You can compile individual files without main, but you cannot link them and of course cannot run them since they are not complete programs. Note that valgrind is not a static analysis tool but a runtime tool, and therefore it is useless on individual translation units not linked into a runnable program.

    If you want to test individual files, a common practice is to include something like the following in each file:

    #ifdef UNIT_TEST
    int main(int argc, char **argv)
    {
        /* unit test code goes here */
    }
    #endif
    

    And compile the file with -DUNIT_TEST.

提交回复
热议问题