extern

Calling C code from C++ in a CMake project. Undefined symbol. Have extern C

£可爱£侵袭症+ 提交于 2020-07-16 10:29:47
问题 I'm trying to build a CMake project that calls C code from C++, and I'm getting undefined symbols, even though I'm (AFAIK) properly using "extern C". CMakeLists.txt: cmake_minimum_required(VERSION 3.0) project(CTest LANGUAGES CXX) add_executable(test main.cpp lib.c) main.cpp: #include "lib.h" int main() { printit(); return 0; } lib.c: #include <stdio.h> #include "lib.h" int printit() { printf("Hello world\n"); return 0; } lib.h: extern "C" int printit(); That gives me an "undefined reference

Rationale of static declaration followed by non-static declaration allowed but not vice versa

◇◆丶佛笑我妖孽 提交于 2020-07-15 11:50:12
问题 This code will compile and is well defined under current C standards: static int foo(int); extern int foo(int); The standard specifies that in this situation ( C11: 6.2.2 Linkages of identifiers (p4) ): For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, 31) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage

Using static keyword in definition vs declaration in C

て烟熏妆下的殇ゞ 提交于 2020-06-27 17:21:54
问题 The following compiles fine, using static only during declaration of function: #include <stdio.h> static int a(); int a(){ return 5; } int main(){ printf("%d\n", a()); return 0; } As a side note, same behaviour as above happens with inline functions, i.e only the declaration could have the keyword. However the following fails, doing the same but on a variable: #include <stdio.h> static int a; int a = 5; int main(){ printf("%d\n", a); return 0; } Getting thew error: non-static declaration of

Using static keyword in definition vs declaration in C

蓝咒 提交于 2020-06-27 17:21:06
问题 The following compiles fine, using static only during declaration of function: #include <stdio.h> static int a(); int a(){ return 5; } int main(){ printf("%d\n", a()); return 0; } As a side note, same behaviour as above happens with inline functions, i.e only the declaration could have the keyword. However the following fails, doing the same but on a variable: #include <stdio.h> static int a; int a = 5; int main(){ printf("%d\n", a); return 0; } Getting thew error: non-static declaration of

Explicit instantiation declaration of header only template(extern template)

心已入冬 提交于 2020-06-24 07:33:12
问题 I am trying to speed up the compile time of the GLM(OpenGL Mathematics). GLM makes heavy usages of C++ templates. This is what I have tried so far. math.h #pragma once #include <glm\glm.hpp> extern template struct glm::tvec3<float, glm::highp>; math.cpp #include "math.h" template struct glm::tvec3<float, glm::highp>; And then I have three files that are using the glm::vec3 template, glm::vec3 is a typedef of glm::tvec3<float, glm::highp> . The three files a,b,c looks almost the same: a.cpp, b

Do i really needed accessor functions to access global variable from another file?

点点圈 提交于 2020-05-17 05:59:07
问题 In my code (game engine code) there are multiple source (.c) files which maintain the status of the game, status like START CONFIGURE STOP END DEFAULT RUNNING for maintaining state, one global variable gameStatus used which shared between multiple source files using extern keyword. now I have read that the global variable is bad to use and it allows the outside module to change it and as the number of components using global variable increases, the complexity of the interactions can also

typedef重复定义 和 error: ‘long long long’ is too long for GCC

大兔子大兔子 提交于 2020-04-07 15:10:35
今天发现一个很有意思的编译问题,然后在Stack Overflow上也有看到类似的。就是出现了 long long long 类型错误提示 错误提示如下: /home/yejy/algorithm_and_data_structure/main.cpp:50:17: error: ‘long long long’ is too long for GCC #define INT64 long long ^ 顾名思义,一个long占4个字节,两个就是8字节,总共64位,等于系统是64位的,如果你使用3个long那就96位了,那肯定会有问题,正常情况下也没人会定义三个long。 ``` #define INT64 long long ``` 然后看代码出错的地方,就是一个宏定义,怎么会出现问题呢? 然后仔细看了一下代码发现是 链接外部库 导致的,工程 A 链接了 B_lib.so 和 C_lib.so 两个动态库, 然后 B 中用宏定义了 long long , C 中使用typedef重新命名了 long long,顺序刚好是宏定义在前,等价于下面两句代码: ``` #define INT64 long long typedef long long INT64; <p style="font-size: 15px; text-indent:2em; letter-spacing:1px

C++Primer学习笔记(一)

可紊 提交于 2020-04-07 05:37:13
1.c++程序有如下后缀: prog.cc prog.cxx prog.cpp prog.cp prog.c 2.标准库定义了4个IO对象: istream类型对象: cin(see-in):处理输入 ostream类型对象: cout(see-out):处理输出 cerr(see-err,标准错误):输出警告和错误信息给用户 clog(see-log):产生程序执行的一般信息 3.endl(操纵符)写入输出流时: 输出换行并刷新缓冲区 4.作用域操作符(::) 5.读入未知数目的输入: (示例:未知数目数据求和) 1 #inlude <iostream> 2 using namespace std; 3 void main() 4 { 5 int sum, value; 6 while(cin>>value) 7 { 8 sum+=value; 9 } 10 cout<<"Sum is: "<<sum<<endl; 11 } 6.标准库头文件用<>,非标准库头文件用"" 7.C++算术类型: bool 布尔型 —— char 字符型 8 wchar_t 宽字符型 16 short 短整型 16 int 整型 16 long 长整型 32 float 单精度浮点型 6位有效数字 double 双精度浮点型 10位有效数字 long double 扩展精度浮点型 10位有效数字

extern关键字

百般思念 提交于 2020-04-06 10:03:23
参考: http://c.biancheng.net/view/404.html 1、在一个文件中: #include <stdio.h> int max(int x,int y); int main(void) { int result; /*外部变量声明*/ extern int g_X; extern int g_Y; result = max(g_X,g_Y); printf("the max value is %d\n",result); return 0; } /*定义两个全局变量*/ int g_X = 10; int g_Y = 20; int max(int x, int y) { return (x>y ? x : y); } 输出:the max value is 20 2、在不同文件中: /****max.c****/ #include <stdio.h> /*外部变量声明*/ extern int g_X ; extern int g_Y ; int max() { return (g_X > g_Y ? g_X : g_Y); } /***main.c****/ #include <stdio.h> /*定义两个全局变量*/ int g_X=10; int g_Y=20; int max(); int main(void) { int result;