使用main函数从.c文件调用c函数

时间:2021-02-17 19:59:06

I have two .c files that both have mains. One of the files has a function that I would like to use in the other main. Is it possible to reference this other function without copying and pasting it into the other .c file?

我有两个都有主电源的.c文件。其中一个文件有一个我想在另一个主要使用的功能。是否可以引用此其他功能而无需将其复制并粘贴到另一个.c文件中?

2 个解决方案

#1


3  

No you don't need to copy and paste, suppose you have this

不,你不需要复制和粘贴,假设你有这个

program-one.c
First program.

program-one.c第一个程序。

#include "common.h" /* This should be implemented */

int main(void)
{
    do_program_one_stuff();
    common_function();
    return 0;
}

program-two.c
Second program.

program-two.c第二个程序。

#include "common.h" /* This should be implemented */

int main(void)
{
    do_program_two_stuff();
    common_function();
    return 0;
}

You need a third .c file and a .h file, like this

你需要第三个.c文件和一个.h文件,就像这样

common.c
Common Functions Implementation.

common.c常用功能实现。

void common_function()
{
    /* Do it here */
}

common.h
Common Functions Header.

common.h常用函数标题。

void common_function();

You now can compile a single binary for each program consisting of two files, the program specific .c file and common.c.

您现在可以为每个程序编译一个二进制文件,该程序由两个文件组成,即程序特定的.c文件和common.c。

The right way to do it is to have a Makefile and generate object code first, and then link the object files togeather, thus compiling each file only once.

正确的方法是首先使用Makefile并生成目标代码,然后将目标文件链接到togeather,从而只编译每个文件一次。

Makefile
This is a GNU make Makefile using gcc as the compiler.

Makefile这是一个使用gcc作为编译器的GNU make Makefile。

CC      = gcc
CFLAGS  = -Wall -Wextra -Werror -g3 -O0 # enable debug symbols and warnings
LDFLAGS = # linker flags here ...
OBJ     = common.o program-one.o program-two.o

all:
    $(CC) $(LDFLAGS) common.o program-one.o -o program-one
    $(CC) $(LDFLAGS) common.o program-two.o -o program-two

%.o: %.c
    $(CC) $(CFLAGS) -c $<

clean:
    @rm -fv *.o program-one program-two

EDIT: in response to your comment I would suggest the following

编辑:作为对您的评论的回应,我建议如下

#define main ignore /* Or whatever name you want */
#include "the-student-implementation.c"
#undef main

int main(void)
{
    /* This would be your `main()' */
    return 0;
}

#2


2  

The best solution is what iharob suggested, but if for some reason that isn't possible, you could surround the main() in the file containing the common function with #ifdef USE_MAIN, then only define the USE_MAIN identifier in the command to build that project. When you build the other project that doesn't have USE_MAIN defined, the preprocessor will cause the second main() to be skipped, so the compiler won't be confused.

最好的解决方案是iharob建议的,但如果由于某种原因不可能,你可以用#ifdef USE_MAIN包含包含公共函数的文件中的main(),然后只在命令中定义USE_MAIN标识符来构建项目。当您构建另一个没有定义USE_MAIN的项目时,预处理器将导致跳过第二个main(),因此编译器不会混淆。

But unless this is really needed, I highly recommend splitting this into three files: main1.c, main2.c, and common.c/common.h

但除非确实需要,否则我强烈建议将其拆分为三个文件:main1.c,main2.c和common.c / common.h

#1


3  

No you don't need to copy and paste, suppose you have this

不,你不需要复制和粘贴,假设你有这个

program-one.c
First program.

program-one.c第一个程序。

#include "common.h" /* This should be implemented */

int main(void)
{
    do_program_one_stuff();
    common_function();
    return 0;
}

program-two.c
Second program.

program-two.c第二个程序。

#include "common.h" /* This should be implemented */

int main(void)
{
    do_program_two_stuff();
    common_function();
    return 0;
}

You need a third .c file and a .h file, like this

你需要第三个.c文件和一个.h文件,就像这样

common.c
Common Functions Implementation.

common.c常用功能实现。

void common_function()
{
    /* Do it here */
}

common.h
Common Functions Header.

common.h常用函数标题。

void common_function();

You now can compile a single binary for each program consisting of two files, the program specific .c file and common.c.

您现在可以为每个程序编译一个二进制文件,该程序由两个文件组成,即程序特定的.c文件和common.c。

The right way to do it is to have a Makefile and generate object code first, and then link the object files togeather, thus compiling each file only once.

正确的方法是首先使用Makefile并生成目标代码,然后将目标文件链接到togeather,从而只编译每个文件一次。

Makefile
This is a GNU make Makefile using gcc as the compiler.

Makefile这是一个使用gcc作为编译器的GNU make Makefile。

CC      = gcc
CFLAGS  = -Wall -Wextra -Werror -g3 -O0 # enable debug symbols and warnings
LDFLAGS = # linker flags here ...
OBJ     = common.o program-one.o program-two.o

all:
    $(CC) $(LDFLAGS) common.o program-one.o -o program-one
    $(CC) $(LDFLAGS) common.o program-two.o -o program-two

%.o: %.c
    $(CC) $(CFLAGS) -c $<

clean:
    @rm -fv *.o program-one program-two

EDIT: in response to your comment I would suggest the following

编辑:作为对您的评论的回应,我建议如下

#define main ignore /* Or whatever name you want */
#include "the-student-implementation.c"
#undef main

int main(void)
{
    /* This would be your `main()' */
    return 0;
}

#2


2  

The best solution is what iharob suggested, but if for some reason that isn't possible, you could surround the main() in the file containing the common function with #ifdef USE_MAIN, then only define the USE_MAIN identifier in the command to build that project. When you build the other project that doesn't have USE_MAIN defined, the preprocessor will cause the second main() to be skipped, so the compiler won't be confused.

最好的解决方案是iharob建议的,但如果由于某种原因不可能,你可以用#ifdef USE_MAIN包含包含公共函数的文件中的main(),然后只在命令中定义USE_MAIN标识符来构建项目。当您构建另一个没有定义USE_MAIN的项目时,预处理器将导致跳过第二个main(),因此编译器不会混淆。

But unless this is really needed, I highly recommend splitting this into three files: main1.c, main2.c, and common.c/common.h

但除非确实需要,否则我强烈建议将其拆分为三个文件:main1.c,main2.c和common.c / common.h