在C中链接具有多个主要功能的文件 [英] Linking files having multiple main function in c
问题描述
是否可以使用makefile链接两个C文件,而makefile可以调用彼此的函数,但是两者都具有自己的main
函数.
Is it possible to link two C files using makefile which calls each other function but both have a main
function of their own.
例如:
C1.c使用C2.c中的函数f()
,但都具有main
函数,我希望C1.c中的main
仅被视为main
.
e.g:
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.
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}
现在terp.c具有main
,而lex.c也具有main
.我只想考虑lex.c的main
.
Now terp.c has a main
and lex.c also has a main
. I want only the main
for lex.c to be considered.
推荐答案
这将特定于您使用的链接器.在Linux上,您可以将--allow-multiple-definition
标志传递给链接器,以便在有多个定义的情况下仅使用第一个符号:
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:
${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.
这篇关于在C中链接具有多个主要功能的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!