extern

LNK2005错误——重复定义错误

↘锁芯ラ 提交于 2019-12-09 15:27:43
造成LNK2005错误主要有以下几种情况: 1.重复定义全局变量。 可能存在两种情况: A、对于一些初学编程的程序员,有时候会以为需要使用全局变量的地方就可以使用定义申明一下。其实这是错误的,全局变量是针对整个工程的。 正确的应该是在一个CPP文件中定义如下:int g_Test;那么在使用的CPP文件中就应该使用:extern int g_Test即可,如果还是使用int g_Test,那么就会产生LNK2005错误,一般错误错误信息类似:AAA.obj error LNK2005 int book c? book@@3HA already defined in BBB.obj。 切记的就是不能给变量赋值否则还是会有LNK2005错误。 这里需要的是“声明”,不是“定义”! 根据C++标准的规定,一个变量是声明,必须同时满足两个条件,否则就是定义: (1)声明必须使用extern关键字; (2)不能给变量赋初值 下面的是声明: extern int a; 下面的是定义 int a; int a = 0; extern int a =0; B、对于那么编程不是那么严谨的程序员,总是在需要使用变量的文件中随意定义一个全局变量,并且对于变量名也不予考虑,这也往往容易造成变量名重复,而造成LNK2005错误。 2.头文件的包含重复。 往往需要包含的头文件中含有变量、函数、类的定义

extern declaration and function definition both in the same file

余生长醉 提交于 2019-12-09 14:52:13
问题 I was just browsing through gcc source files. In gcc.c , I found something like extern int main (int, char **); int main (int argc, char **argv) { Now my doubt is extern is to tell the compiler that the particular function is not in this file but will be found somewhere else in the project. But here, definition of main is immediately after the extern declaration. What purpose is the extern declaration serving then? It seems like, in this specific example, extern seems to be behaving like

Xcode编译Undefined symbols for architecture xxx 错误总结

╄→尐↘猪︶ㄣ 提交于 2019-12-09 14:17:39
领个红包,支持一下作者 这是支付宝推出的一个最新活动,用支付宝扫码即有红包送,0元到99元不等,红包可以到店支付使用,每日仅可领一次。你每用一个红包,作者即可得到支付宝送出的1毛赏金,如果你觉得这篇文章对你有用,那不如扫码支持一下作者吧! 每次遇到这种错误就头痛,不知道要害死多少脑细胞了,就在这里做个总结吧 可能会遇到这几种错误: Undefined symbols for architecture armv7 Undefined symbols for architecture armv7s Undefined symbols for architecture arm64 Undefined symbols for architecture i386 Undefined symbols for architecture x86_64 错误原因分析 0、如果只有 @interface ,没有@implementation也会导致这个错误 1、注意看Build Phases里面的Compile Sources里面有没有被报错的类文件,如果没有的话也会报上面的错误 2、大部分情况下是忘记添加了某个系统framework或dylib吧,比如你在项目中使用了sqlite3,但是没有添加libsqlite3.dylib,就会出现这个问题。解决办法是增加对应的framework或dylib。

extern使用注意事项与语法简述

谁都会走 提交于 2019-12-09 13:12:58
1.使用extern时要严格对应声明的格式,定义的是数组类型,extern char a[];而不能是extern char *a; 2.如果要在*.c文件中引用另一个文件中的一个全局变量,那就应该放在*.h中用extern来声明这个全局变量。 3.函数声明和定义前面extern没有实际的意义(除非不想在对应头文件声明 —— 省略头文件),如果该函数不想被调用可以添加static关键词 4.根据编译器不同,有些编译器可以链接(extern)常量:const float pi = 3.14; extern const flaot pi; 5.extern 可以链接外部变量,当我们两个文件同时引用同一个全局变量的时候,只在一个文件定义,编译时会在未定义的文件报错;在两个文件都定义,编译通过,链接时会包定义冲突的错误,解决办法就是其中一个文件定义,另一个文件extern来告诉编译器这个变量在其他文件定义过了,自个儿去找(^..^)。当然也可以通过头文件的方式来实现,变量定义在头文件,连个引用变量的.c文件均include有该头文件。 6.extern "c",在C++的环境,想要兼容C,一般都是需要extern "C"。格式: #ifdef __cplusplus #if __cplusplus extern "C"{ #endif #endif /* __cplusplus */ …

在WPF(C#)工程中使用C导出动态库DLL

会有一股神秘感。 提交于 2019-12-09 02:52:43
运行环境:Visual Studio 2017 一.创建动态库 1.建立tteHwif.dll项目 项目创建完成 2.tteHwif.cpp // DLLshow.cpp: 定义 DLL 应用程序的导出函数。 // #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "time.h" #include "tteHwif.h" //启动自检 bool _stdcall tteStartSelfCheck() { if (rd() > 5 ) return true ; else return false ; } //获取自检状态 bool _stdcall tteGetSelfCheckStatus( bool * status) { if (rd() > 2 ) { if (rd() > 2 ) *status = true ; else *status = false ; return true ; } else return false ; } //获取同步信息 bool _stdcall tteGetSyncStatus( bool * syncStatus, bool * lanAStatus, bool * lanBStatus, unsigned int * timeOffset) {

对声明和定义的理解

爱⌒轻易说出口 提交于 2019-12-08 23:34:37
  可以先看看 这篇博客 。   一个 变量 或 函数 可以 被声明无数次 ,但是却 至多只能被定义一次 ,且在该变量或函数 在使用时 , 必须被定义一次 。(若该函数/变量不被使用,则可以只声明不定义)   一、判断一条语句是否为 变量的声明 :若变量前有 extern 关键字 ,则它为 声明 ,若变量前 没有 extern 关键字 ,则其为 定义 : extern int i; //声明i int i; //定义i,但未对i进行初始化 int i = 5; //是一个定义,同时对i进行了初始化。   总之:判断一条语句是否为声明的声明, 关键是看该语句中是否包含 extern 关键字 , 而非 看该语句中是否包含”=”, ”=” 的作用是对变量进行初始化 。   一个例外: extern int i = 5 ; 该语句是一个定义 ,这句话的作用是定义一个全局的int类型变量,变量名为i,i的初值为5。   不能在函数体中使用该语句,会报错: error C2205: 'i': cannot initialize extern variables with block scope。 只能在函数外部使用,作用与“int i = 5;“相同。 所以” extern int i = 5 “这条语句的价值不大。      二、判断一条语句是否为 函数声明 : 若函数包含了实现体 ( 即“

C#: Implementation of, or alternative to, StrCmpLogicalW in shlwapi.dll

可紊 提交于 2019-12-08 14:40:27
问题 For natural sorting in my application I currently P/Invoke a function called StrCmpLogicalW in shlwapi.dll. I was thinking about trying to run my application under Mono, but then of course I can't have this P/Invoke stuff (as far as I know anyways). Is it possible to see the implementation of that method somewhere, or is there a good, clean and efficient C# snippet which does the same thing? My code currently looks like this: [SuppressUnmanagedCodeSecurity] internal static class

How to Creat DLL(Dynamic link library)

落花浮王杯 提交于 2019-12-07 18:54:31
该文章属于在YouTube视频上看到的,链接如下: https://www.youtube.com/watch?v=EmDJsl7C9-k&t=3s 1.创建一个工程并建立一个控制台程序 2.Solution-->右键新建dll工程 3.Solution-->右键属性,选择依赖项,确定 4.CppClient-->右键设置属性$(SolutionDir)myLib\,inherit打勾,确定 5.VC++Directories-->Library Directories-->$(SolutionDir)$(IntDir) 6.myLib-->右键设置属性-->Command Line-->/DDLL_BUILD 7.myLib添加一个类,再添加一个头文件myLib.h 8.代码如下: 1 #pragma once 2 3 #ifndef EXT_MYLIB 4 5 #ifdef DLL_BUILD 6 #define EXT_MYLIB __declspec(dllexport) 7 #else 8 #pragma comment(lib, "myLib.lib") 9 #define EXT_MYLIB __declspec(dllimport) 10 #endif 11 12 #endif 13 14 15 extern int EXT_MYLIB max_size; 16

What is a concept behind using extern in c/c++?

情到浓时终转凉″ 提交于 2019-12-07 10:58:57
问题 Sorry for this type of question. But, I am very curious about the keyword extern in C\C++ . while searching for explanation for extern I got to know that extern tell the compiler that the variable or function is already defined in some other file or program. But if this is a case then why we use extern ? as I tried some codes as follows: extern int var; int main(void) { var = 10; return 0; } This code is giving me an error message as unresolved external symbol "int var" (?var@@3HA) . and if I

C语言中memset-memcpy(memmove memccpy)-strcpy函数源代码

主宰稳场 提交于 2019-12-07 09:18:46
一、概述 1、memset 原型:extern void *memset(void *buffer, char c, int count); 用法:#include <string.h> 功能:把buffer所指内存区域的前count个字节设置成字符c 说明:返回指向buffer的指针 2、 memcpy 原型:extern void *memcpy(void *dest, void *src, unsigned int count); 用法:#inclue <string.h> 功能:由src所指内存区域复制count个字节到dest所指内存区域 说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针 memccpy 原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count); 用法:#include <string.h> 功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制 说明:返回指向字符ch后的第一个字符的指针,如果src前count个字节中不存在ch则返回NULL。 memmove 原型:extern void *memmove(void *dest, const void *src,