在CI中获得链接器错误“ main”的多个定义。是的,的确如此,但是:
为什么链接程序为什么尝试包含第二个(ext.c)主函数,尽管我刚刚包含了头文件ext.h?我希望链接器仅链接已找到其原型或初始main所需的函数?
我该如何解决以下问题:a)测试可以编译并链接而没有问题(只需使用ext.c中的func())和b)ext.c也可以作为独立的应用程序进行编译和链接?
(示例)代码:
//file: test.c
#include "/home/stefanm/test/test.h"
void main (int argc, char * argv[])
{
    uint8_t var = 123;
    printf ("main(): var= %i\n", var);
    func (var);
                                                                                                                    }
//file: test.h
#ifndef TEST_H
#define TEST_H
#include <the rest>
#include "/home/stefanm/test/ext.h"                                                                                                                     
#endif
...以及外部模块:
//file: ext.c
#include "/home/stefanm/test/ext.h"
uint8_t func (uint8_t i){    
    printf ("func(): Variable i is %i", i); 
    return 0;
}
void main () {
    printf ("ext main func");
}   
//file: ext.h
#ifndef EXT_H
#define EXT_H
#include "all needed headers"  
uint8_t func (uint8_t);
#endif    
我用 gcc test.c ext.c -o test
您的外部模块应该没有,main()因为它是模块而不是应用程序。您应该只main()从模块移动到单独的文件:
//file: app.c
#include "/home/stefanm/test/ext.h" // <-- BTW, using absolute paths is not a good idea
void main () {
    //use function from ext here
    printf ("app main func");
}
然后像这样编译您的应用程序:
gcc app.c ext.c
和你的测试是这样的:
gcc test.c ext.c