我的档案是
// main.c
#include "add.c"
int main(void) {
int result = add(5,6);
printf("%d\n", result);
}
Run Code Online (Sandbox Code Playgroud)
和
// add.c
int add(int a, int b) {
return a + b;
}
Run Code Online (Sandbox Code Playgroud) 在对某些C代码应用单元测试时,我们遇到了一个问题,即在不修改源代码的情况下,无法在测试文件中调用某些静态函数.有没有简单或合理的方法来解决这个问题?
我想在另一个文件中包含.c文件.有可能吗?如果我在.c文件中包含头文件,它可以正常工作,但如果我在另一个.c文件中包含.c文件,它也不能正常工作.
我正在使用Visual Studio,我收到以下错误:
main.obj : error LNK2005: _sayHello already defined in sayHello.obj
/* main.c */
#include "sayHello.c"
int main()
{
return 0;
}
/* sayHello.c */
#include <stdio.h>
void sayHello()
{
printf("Hello World");
}
Run Code Online (Sandbox Code Playgroud)
我不知道这个错误可能意味着什么.是时候问更高级的C编码员了.:)
是否有可能无法从其他文件访问 ANSI C 中的函数?功能如何以及何时具有受限访问权限?起初我认为如果一个函数没有包含在任何标题中,它就是私有的。但似乎并非如此。
我正在考虑仅标头设计与标头和源设计。我不确定标头和源是否允许编译器跨目标文件和跨链接进行优化?例如内联优化?
例如:
file1.c 具有:
static const struct
{
int a;
int b;
int c;
} mystruct = { 1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
file2.c 具有:
#include <stdio.h>
#include "file1.c"
Run Code Online (Sandbox Code Playgroud)
等等.
这样可以吗?
c ×6
ansi-c ×1
external ×1
function ×1
gcc ×1
header-files ×1
include ×1
optimization ×1
struct ×1
unit-testing ×1