linkage

extern enum in c++

心不动则不痛 提交于 2019-12-21 04:22:18
问题 I have an enum I have declared in some .h file: typedef enum { NONE, ONE, TWO, THREE } MYENUM; in a seperate .cpp I cannot do this: extern enum MYENUM; //works extern MYENUM TWO; //makes sence, TWO is not an INSTANCE of MYENUM... how would one do so without including the whole header where the enum is declared? 回答1: You can't use an incomplete type. You can only pass around pointers to it. This is because until the type is completed, the compiler doesn't know how big it is. OTOH a pointer is

Static functions declared in “C” header files

痞子三分冷 提交于 2019-12-20 12:14:09
问题 For me it's a rule to define and declare static functions inside source files, I mean .c files. However in very rare situations I saw people declaring it in the header file. Since static functions have internal linkage we need to define it in every file we include the header file where the function is declared. This looks pretty odd and far from what we usually want when declaring something as static. On the other hand if someone naive tries to use that function without defining it the

Why does declaring an extern variable inside main() works,but not defining it inside main() as well?

北战南征 提交于 2019-12-19 09:24:31
问题 This seems very trivial but a somewhat rigorous explanation for the following behavior will help my understanding of extern a lot.So I'll appreciate your answers. In the following sample program,I've declared an extern variable x inside a function ( main() ).Now if I define the variable at file scope right after main() and assign 8 to it, then the program works fine and 8 is printed.But if I define the variable x inside main() after the printf() ,expecting the extern declaration to link to it

Do classes have external linkage?

百般思念 提交于 2019-12-18 13:16:48
问题 I have 2 files A.cpp and B.cpp which look something like A.cpp ---------- class w { public: w(); }; B.cpp ----------- class w { public: w(); }; Now I read somewhere (http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr082.htm) that classes have external linkage. So while building I was expecting a multiple definition error but on the contrary it worked like charm. However when I defined class w in A.cpp, I got the redefinition

Program statically linked to a library but still needs dll to run

不羁的心 提交于 2019-12-18 10:53:10
问题 There are things that I don't understand when it comes to linking... I'm writing a program using a 3rd party library (the GEOS library). This program has a dependency to geos.lib but still needs geos.dll to run. I read this question, I think I understand the difference between static and dynamic libraries. What I don't understand is why I still need a dll when I statically link a library. 回答1: It's not statically linked. The .lib is just a stub library that binds in the .dll on windows. That

Linkage of various const/static variables

二次信任 提交于 2019-12-18 10:46:36
问题 I have a few questions about the linkage from the following variables. By examples of 7.1.1/7 of C++03 and experimenting with compilers (Comeau, Clang and GCC), I came to the following linkage kinds: First static , then extern static int a; // (a) extern int a; // (b) valid, 'a' still internal It's clear to me with accordance to section 3.5: (a) implies internal linkage. And (b) also implies internal linkage, because the name "a" is declared static (by (a)). First extern , then static extern

Adding a Symbolic Link in the Application Bundle

只谈情不闲聊 提交于 2019-12-17 21:06:05
问题 Apple specifies in the UILocalNotification class reference that the audio file must be from the application bundle. However, some smart geek has found his way around this limitation by using: // this works by going up the bundle dir, then pointing to the Documents dir localNotif.soundName = @"../Documents/blabla.caf"; This workaround has worked well with iOS 5, however, it broke in iOS 6. In a desperate attempt to try and come up with a new workaround, I made an alias (Symbolic Link) called

C++ standard: do namespace-scoped constexpr variables have internal linkage?

若如初见. 提交于 2019-12-17 19:35:00
问题 Imagine we have a header foo.h containing the following: #ifndef FOO_H_ #define FOO_H_ namespace foo { constexpr std::string_view kSomeString = "blah"; } #endif // FOO_H_ Is foo::kSomeString guaranteed to have internal linkage in any translation unit that includes foo.h ? Does this vary between C++11 and C++17? In the draft standard [basic.link]/3 says A name having namespace scope has internal linkage if it is the name of [...] a non-inline variable of non-volatile const-qualified type that

Can a variable be declared both static and extern?

我与影子孤独终老i 提交于 2019-12-17 16:35:27
问题 Why the following doesn't compile? ... extern int i; static int i; ... but if you reverse the order, it compiles fine. ... static int i; extern int i; ... What is going on here? 回答1: This is specifically given as an example in the C++ standard when it's discussing the intricacies of declaring external or internal linkage. It's in section 7.1.1.7, which has this exert: static int b ; // b has internal linkage extern int b ; // b still has internal linkage extern int d ; // d has external

static vs extern “C”/“C++”

亡梦爱人 提交于 2019-12-17 06:35:38
问题 What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just lucky or... class X { public: static void proxy(int i) {} } makecontext(..., (void (*)(void)) X::proxy, ...); vs extern "C" void proxy(int i) {} makecontext(..., (void (*)(void))