0

I am restricted by very specific (and rather senseless...) filing system and I can't create header files or split existing files into files holding only functions and only execution calls. I can only create .c files that can be executed and have some sort of output.

A lot of code can be reused but I am being forced to copy some functions from file to file.

Is it possible to cure this mess by including but not compiling one or more file or omitting some functions in the included files? Maybe adding some debugging into the mix can allow to compile only part of included files?

4

2 回答 2

2

像这样放置共享代码,以便它可以自己编译,同时您可以从其他 .c 文件中包含它而不会获得重复的主要功能:

 // shared.c
 #ifndef SHARED_C
 #define SHARED_C

 #if __INCLUDE_LEVEL__ == 0
 #    include <stdio.h>
 #    include <stdlib.h>
 int main() { 
     fprintf(stderr, "shared file, not to run!\n");
     return EXIT_FAILURE;
 }

 #endif


 int shared_func() { return 1; }

 #endif

并从其他文件中使用它

#include "shared.c"
int x = shared_func();
于 2013-05-10T23:55:45.463 回答
0

你可以使用预处理器吗?就像是:

// this is file1.c
int Foo(int bar);
#ifndef HEADERS
int Foo(int bar)
{
    return 42;
}
#endif

// this is file2.c
#define HEADERS
#include "file1.c"

Foo(42);
于 2013-05-10T23:53:26.343 回答