我正在尝试使用extern变量.
它抱怨因为使用numberWithInt我没有传递一个包含作为我变量的值
所以我删除了const并且它抱怨extern变量必须是常量,那么这里的解决方案是什么?
我不想使用INT
.h
extern NSNumber const *MoveID;
.m
NSNumber const *MoveID = [NSNumber numberWithInt:1];
Run Code Online (Sandbox Code Playgroud) 当在不同的文件中定义结构时,我在尝试使结构正常工作时遇到了一些麻烦.据我所知,错误告诉我结构被定义了两个不同的时间.我相信也许我可能需要在某处使用extern?我尝试过试验并在Google上寻求帮助,但无济于事.
任何帮助都将非常感谢,谢谢.我的所有四个文件都在下面.
文件:Foo.h
typedef struct
{
int number;
} my_struct; // Redefinition; different basic types
Run Code Online (Sandbox Code Playgroud)
文件:Foo.c
#include "Foo.h"
#include "Bar.h"
#include <stdio.h>
my_struct test;
int main(void)
{
test.number = 0;
DoSomething(&test);
printf("Number is: ", &test.number);
}
Run Code Online (Sandbox Code Playgroud)
文件:Bar.h
#include "Foo.h"
void DoSomething(my_struct *number);
Run Code Online (Sandbox Code Playgroud)
文件:Bar.c
#include "Bar.h"
void DoSomething(my_struct *number)
{
number->number = 10;
}
Run Code Online (Sandbox Code Playgroud) 我相信我会发疯,但请考虑以下C代码:
// file1.c
int first;
void f(void)
{ first = 2; }
Run Code Online (Sandbox Code Playgroud)
// file2.c
#include <stdio.h>
int first;
void f();
int main(void)
{
first = 1;
f();
printf("%d", first);
}
Run Code Online (Sandbox Code Playgroud)
这两个文件,由于某种原因将编译和链接在一起,并打印2.我一直的印象是,除非我标记一个或另一个(但不是两者)的定义first有extern,这不会编译,这实际上整点extern!
我正在运行以下代码编译为: gcc A.c B.c -o combined
计划A:
#include<stdio.h>
int a=1;
int b;
int main()
{
extern int a,b;
fun();
printf("%d %d\n",a,b);
}
Run Code Online (Sandbox Code Playgroud)
方案B:
int a;
int b=2;
int fun()
{
printf("%d %d\n",a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在运行"组合"程序时,输出为:
1 2
1 2
Run Code Online (Sandbox Code Playgroud)
现在,我对这个问题有些疑惑:
为什么不输出:
0 2
1 0
是不是a和b定义了两次?
请清楚地解释一下,我在了解外部时遇到了很多问题,而且很少有这些疑虑不时出现.
提前致谢.
有没有办法做extern alias一个剃刀(MVC3)视图?
我有两个版本的相同程序集(即1.0和2.0),其类型具有相同的名称和命名空间,我需要一种方法来在剃刀视图中指定较新的版本.
我试过了:
@extern
Run Code Online (Sandbox Code Playgroud)
和:
@{ extern alias MyAlias; }
Run Code Online (Sandbox Code Playgroud)
但这些都没有奏效.
我有一个C++类,我正在用一些C文件编译它.
我想调用一个用C++定义的函数,实际上是在C++类中,所以我该怎么做?
以下声明显示我在说什么:可能存在语法错误:
serial_comm.cpp
class MyClass {
void sendCommandToSerialDevice(int Command, int Parameters, int DeviceId) {
//some codes that write to serial port.
}
}
Run Code Online (Sandbox Code Playgroud)
external.c
int main(int argc, char ** argv) {
//what am I going to write here?
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用makefile来编译其他人编写的程序,使用cygwin.我收到很多错误信息,其中很多都抱怨error: template with C linkage.
在搜索了一下后,似乎问题与之相关extern "C".该行包含在文件cygwin/usr/include/pthread.h中,该文件包含#include < pthread.h >在其中一个标题中.当我删除此行时,大多数错误消息都会消失.但是还有一些,有以下几种:
/usr/include/pthread.h:67:5: error: previous declaration of ‘int pthread_atfork(void (* )(),void ( *)(), void ( *)())’ with ‘C++’ linkage
/usr/include/sys/unistd.h:136:5: error: conflicts with new declaration with ‘C’ linkage
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这一问题?我很乐意坐下来详细了解所有这些东西,但我没有时间需要这个程序运行..
我inline在C99 感到困惑.
这就是我想要的:
.c文件).static inline).C++就是inline这样做的.
但是(如果我错了请纠正我)在C99中没有办法得到这种行为.
我可以使用static inline,但它会导致重复(不同翻译单元中相同功能的地址不一样).我不希望这种重复.
所以,这是我的问题:
inlineC99的理念是什么?参考文献:
inline,但我不明白为什么.这只"仅在一个编译单元中"限制真的那么好吗?inline.我读过它,但我不明白.inline函数的策略.inline在C99中获得C++ 行为(是的,我们可以)head.h
#ifndef __HEAD_H__
#define __HEAD_H__
inline int my_max(int x, int y) {
return (x>y) ? (x) : (y);
}
void call_and_print_addr();
#endif
Run Code Online (Sandbox Code Playgroud)
src.c
#include "head.h"
#include <stdio.h>
// This is necessary! And it should occurs and only occurs …Run Code Online (Sandbox Code Playgroud) 看起来嵌套extern"C"是合法的.例如:
extern "C" extern "C" void foo();
Run Code Online (Sandbox Code Playgroud)
第二个extern "C"基本上被忽略了.这是由C++标准保证的吗?哪里?
I am using the glm library, which is a header-only collection of math utilities intended for 3D graphics. By using -ftime-trace on Clang and ClangBuildAnalyzer, I've noticed that a lot of time is being spent instantiating glm types:
**** Templates that took longest to instantiate:
16872 ms: glm::vec<4, signed char, glm::packed_highp> (78 times, avg 216 ms)
15675 ms: glm::vec<4, unsigned char, glm::packed_highp> (78 times, avg 200 ms)
15578 ms: glm::vec<4, float, glm::packed_highp> (78 times, avg 199 ms)
... …Run Code Online (Sandbox Code Playgroud)