简体   繁体   English

在c中链接具有多个主要功能的文件

[英]Linking files having multiple main function in c

Is it possible to link two C files using makefile which calls each other function but both have a main function of their own. 是否可以使用makefile链接两个C文件,该文件调用彼此的函数,但两者都有自己的main函数。

eg: C1.c uses function f() from C2.c.but both have main function and I want the main in C1.c to be considered as main only. 例如:C1.c使用函数f()从C2.c.but都具有main作用,我想在main在C1.c被视为main只。

FILES.o=set.o hash.o printnfa.o input.o nfa.o dfa.o terp.o minimize.o defnext.o print_ar.o pairs.o squash.o signon.o print.o lex.o assort.o prnt.o printv.o bintoasc.o ferr.o onferr.o fputstr.o pchar.o driver.o searchenv.o hashadd.o esc.o 

PROGRAM= Lexer
INC := -I./debug.h -I./global.h -I./stack.h -I./set.h -I./hash.h
CFLAGS=-DMAIN
all: ${PROGRAM}

${PROGRAM}: ${FILES.o}
${CC} -o $@ ${CFLAGS} $(INC) $^ ${LDFLAGS} ${LDLIBS}

Now terp.c has a main and lex.c also has a main . 现在terp.c有一个main和lex.c也有一个main I want only the main for lex.c to be considered. 我只想要考虑lex.c的main内容。

This will be specific for the linker you use. 这将特定于您使用的链接器。 On Linux you can pass the --allow-multiple-definition flag to the linker in order to use only the first symbol in case of multiple definitions: 在Linux上,您可以将--allow-multiple-definition标志传递给链接器,以便在多个定义的情况下仅使用第一个符号:

${PROGRAM}: ${FILES.o}
${CC} -Xlinker --allow-multiple-definition -o $@ ${CFLAGS} $(INC) $^ ${LDFLAGS} ${LDLIBS}

This will omit all errors about duplicate symbols and cause the linker to ignore any redefinitions. 这将省略有关重复符号的所有错误,并导致链接器忽略任何重定义。 Make sure that the object file containing the symbol you wish to use comes before any redefinitions in the list. 确保包含您要使用的符号的目标文件出现在列表中的任何重新定义之前。

简短的答案......不......这是不可能的

First of all I would recommend that you refactor the code so that you don't have functions that will be used by many programs in the same file as a main function. 首先,我建议您重构代码,这样您就不会有与主函数在同一文件中的许多程序使用的函数。

Any way, here is an example on how to use the pre-processor to include only one of the main functions. 无论如何,这里是一个关于如何使用预处理器仅包含一个主要功能的示例。

We have two source code files both with a main function plus one other function, foo() in file1.c and bar() in file2.c. 我们有两个源代码文件,它们都带有一个main函数和另一个函数,file1.c中的foo()和file2.c中的bar()。 foo() and baa() are called from both of the main functions. 从两个主要函数调用foo()和baa()。

By changing the MAIN_ALT value in the Makefile we can switch between the main functions: 通过更改Makefile中的MAIN_ALT值,我们可以在主要功能之间切换:

Use the main function in file1.c: 使用file1.c中的main函数:

DEFINES += -DMAIN_ALT=1

Use the main function in file2.c: 使用file2.c中的main函数:

DEFINES += -DMAIN_ALT=2

Makefile: Makefile文件:

OBJS = file1.o file2.o
DEFINES += -DMAIN_ALT=1
CFLAGS += -Wall $(DEFINES)
prog:$(OBJS) 
    $(CC) $(CFLAGS) -o $@ $^

file1.c: file1.c中:

#include "stdio.h"
#include "def.h"

void foo(){
    printf("From foo\n");
}

#if MAIN_ALT == 1
int main(){
    printf("Main in file1.c\n");
    bar();
    foo();
    return 0;
}
#endif

file2.c: file2.c中:

#include "stdio.h"
#include "def.h"

void bar(){
    printf("From bar\n");
}

#if MAIN_ALT == 2
int main(){
    printf("Main in file2.c\n");
    bar();
    foo();
    return 0;
}
#endif

def.h: def.h:

#ifndef __DEF_H__
#define __DEF_H__
void foo();
void bar();
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM