我有两个c文件,foo.c的功能和test_foo.c,它们测试foo.c的功能.
有没有办法BAR在不使用头文件的情况下访问test_foo.c中foo.c中定义的struct typedef ?到目前为止,我能够避免使用啊文件,因此整个程序将由foo.c组成.谢谢.
foo.c
typedef struct BAR_{...} bar;
BAR *bar_new(...) {..}
test_foo.c
extern BAR *bar_new(...)
Run Code Online (Sandbox Code Playgroud)
error: expected declaration specifiers or ‘...’ before ‘BAR’
我有一个文件定义了非常基本的IO函数,我想创建另一个使用该文件的文件.
有没有办法将这两个文件联系起来?
prints.asm:
os_return:
;some code to return to os
print_AnInt:
;some code to output an int, including negatives - gets param from stack
print_AChar:
;some code to output a char - gets param from stack
Run Code Online (Sandbox Code Playgroud)
usingPrintTest.asm:
main:
push qword 'a'
call print_AChar ;gets this from prints.asm somehow (that's my question)
call os_return ;and this too..
Run Code Online (Sandbox Code Playgroud)
注意这些不是实际的文件......它们只是用于解释我的问题:)
谢谢!
我来自C#背景,我很难弄清楚如何在Objective C中使用静态变量(在我的例子中是BOOL).我的问题是:
我有一个“unnamed-struct”类型的全局对象,我正在尝试定义它。我不想用这种无用的类型污染我的全局命名空间(它只会使用一次)。
全局.h
extern struct {
int x;
} A;
Run Code Online (Sandbox Code Playgroud)
有没有正确的方法来定义这样的对象?
我正在尝试这个:
全局.cpp
struct {
int x;
} A = { 0 };
Run Code Online (Sandbox Code Playgroud)
但是 VS2012 抛出“错误 C2371:'A':重新定义;不同的基本类型”。谢谢。
我无法理解为什么这不起作用.
extern int i;
int main()
{
printf(" %d ", i);
}
static int i =3;
Run Code Online (Sandbox Code Playgroud)
此外,这不起作用:
extern int i;
static int i =3;
int main()
{
printf(" %d ", i);
}
Run Code Online (Sandbox Code Playgroud)
但是如果static在extern declaration它工作之前定义了变量:
static int i =3;
extern int i;
int main()
{
printf(" %d ", i);
}
Run Code Online (Sandbox Code Playgroud)
正如我所知道的extern int i那样,i它存在于其他地方,在这里它看起来如何(int i)
但是,其他地方意味着:
1)也许,稍后在same翻译单元中指出global variable.
2)也许,在一些other翻译单位.
我认为即使(1)将范围 …
我想知道是否需要手动编写Google闭包编译器的外部文件.我还没有看到任何关于从我的.js文件生成这些文件的可能性.手动创建所有外部设备是相当多的工作,它也容易出错(即错误的参数定义,因为它随时间变化......)
所以我想知道是否有一个Linux工具(命令行)用于此目的.如果没有,是否有办法将.js与extern文件进行比较,这样我至少可以确保它们是同步的.
在C++中,必须初始化引用变量.int&a; //错误
static int &b; // Error
Run Code Online (Sandbox Code Playgroud)
但
extern int &c; // No error
Run Code Online (Sandbox Code Playgroud)
为什么编译器没有给出extern说明符引用的错误?
我想转发声明一个 const 变量而不给它外部链接。然而,在我看来,这是不可能的,因为extern关键字同时意味着“这具有外部链接”和“这是一个变量声明,而不是定义”,而且我无法得到一个没有另一个:
//// main.cpp: ////
extern const char table[256]; // forward declaration. External linkage.
// const char table[256]; // Error: table requires an initializer
// static const char table[256]; // Same error
// foo uses table so I need it forward declared:
char foo()
{
// uses table
}
const char table[256] = {...}; // Actual definition
Run Code Online (Sandbox Code Playgroud)
我的理解正确吗?有什么解决方法吗?
我对为什么在我的 extern.cpp 文件的定义中需要extern或不需要intvs感到困惑char*。我有以下测试程序:
// extern.cpp
extern const int my_int = 1;
const char* my_str = "FOO";
Run Code Online (Sandbox Code Playgroud)
// main.cpp
#include <iostream>
extern const int my_int;
extern const char* my_str;
int main() {
std::cout << my_int;
std::cout << my_str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除externfromextern const int my_int = 1;那么我得到undefined reference to 'my_int'. 如果我将 extern 添加到const char* my_str = "FOO";然后我会收到警告'my_str' initialized and declared 'extern'。我为什么需要extern上my_int,但将它添加到 …