one-definition-rule

In class static const ODR

只愿长相守 提交于 2019-11-30 09:18:24
I am a bit confused by the static in-class initialization of a const member. For example, in the code below: #include <iostream> struct Foo { const static int n = 42; }; // const int Foo::n; // No ODR void f(const int& param) { std::cout << param << std::endl; } int g(const int& param) { return param; } template<int N> void h() { std::cout << N << std::endl; } int main() { // f(Foo::n); // linker error, both g++/clang++ std::cout << g(Foo::n) << std::endl; // OK in g++ only with -O(1,2 or 3) flag, why?! h<Foo::n>(); // this should be fine } Live example I do not define Foo::n (the line is

static keyword in h file and internal linkage

柔情痞子 提交于 2019-11-30 06:56:59
问题 Yet another static question. I have read the following: What are static variables? file scope and static floats http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx And I still fail to understand the following behavior: I have one h file: // StaticTest.h #include <stdio.h> static int counter = 0; struct A { A () { counter++; printf("In A's ctor(%d)\n", counter); } ~A () { counter--; printf("In A's dtor(%d)\n", counter); } }; static A a; And two cpp files: // StaticTest1.cpp #include

Static variable in a Header File

≡放荡痞女 提交于 2019-11-30 04:01:32
Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h file2.cpp I have declared static variable say static int Var1 in both the header files. Both file1.h and file2.h are included in main.cpp file. I did this since the static variable will have file scope so it won't conflict each other. But after compilation I found it is showing conflict. Now static variable is behaving like a extern variable. On the other hand if I declare the static variable in both .cpp files, it compiles well. I am unable to understand this behavior. Can any body explain how scope and

One definition rule in c++

[亡魂溺海] 提交于 2019-11-29 18:03:10
According to the c++ standard: No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template. //--translation_unit.cpp--// int a; void foo() { int a; //Second defention of a. ODR fails. } Can you explain me how ODR does work actually? This doesn't break the rule because you define two different variables. They have the same name, but are declared in different scopes, and so are separate entities. Each has a single definition. The declaration in the function's scope is said to hide the one in the global namespace. Within the

How to run ranlib on an archive built through Android.mk?

此生再无相见时 提交于 2019-11-29 15:48:49
This has come up on a couple of libraries I work with regularly. See, for example: Error SSL archive symbol table (run ranlib) no archive symbol table (run ranlib) while building libcryptopp.a through ndk-build In the questions, the users created an Android.mk for the OpenSSL and Crypto++ libraries. The pain point seems to be users adding the Android.mk wrapper to the sources. Outside of Android, each project is Makefile based, each project builds a static archive, and each project builds a shared object based on the static archive. Each project also runs ranlib on the static archive. Crypto++

Inline constructors and One Definition Rule

 ̄綄美尐妖づ 提交于 2019-11-29 11:44:56
Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X() { cout << "1" << endl; } }; void bar(); void foo() { X x; } int main() { foo(); bar(); return 0; } 2.cpp #include <cstdio> struct X { X() { printf("2\n"); } }; void bar() { X x; } Is program compiled from these files well-formed? What should be in it's output? I've expected linker error due to violation of One Definition Rule or output "1 2". However it prints out "1 1" when compiled with g++ 3.4 and VC 8.0. How this can be explained? This does violate ODR (3.2) - specifically that you can have more

Static variable in a Header File

你离开我真会死。 提交于 2019-11-29 01:42:33
问题 Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h file2.cpp I have declared static variable say static int Var1 in both the header files. Both file1.h and file2.h are included in main.cpp file. I did this since the static variable will have file scope so it won't conflict each other. But after compilation I found it is showing conflict. Now static variable is behaving like a extern variable. On the other hand if I declare the static variable in both

static keyword in h file and internal linkage

帅比萌擦擦* 提交于 2019-11-28 23:28:28
Yet another static question. I have read the following: What are static variables? file scope and static floats http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx And I still fail to understand the following behavior: I have one h file: // StaticTest.h #include <stdio.h> static int counter = 0; struct A { A () { counter++; printf("In A's ctor(%d)\n", counter); } ~A () { counter--; printf("In A's dtor(%d)\n", counter); } }; static A a; And two cpp files: // StaticTest1.cpp #include "StaticTest.h" int main () { return 0; } And: // StaticTest2.cpp #include "StaticTest.h" The output of the

c & c++ default global variable linkage, multiple declaration & definition problem

空扰寡人 提交于 2019-11-28 21:10:19
For example: code1.c / .cpp int a; // ... and so on code2.c / .cpp int a; int main(void) { return 0; } go to compile: $gcc code1.c code2.c # this is fine $ $g++ code1.cpp code2.cpp # this is dead /tmp/ccLY66HQ.o:(.bss+0x0): multiple definition of `a' /tmp/ccnIOmPC.o:(.bss+0x0): first defined here collect2: ld returned 1 exit status Is there any global variable linkage difference between C & C++? CB Bailey It's not strictly legal. int a; is a tentative definition in C. You are allowed multiple tentative definitions and at most one non-tentative definition per translation unit of each object

Why is multiple definition of a const global variable allowed in C++ and not in C?

夙愿已清 提交于 2019-11-28 18:15:22
Multiple definition of a global variable is not allowed in C or C++ due to the One Definition Rule. However, in C++ a const global variable can be defined in multiple compilation units with no error. This is not the same as in C. Why does C++ allow this while C does not? Why does the usage and behaviour of a const global differ from a non-const global in this way in C++ compared to C? What is happening under the covers with C++ and C with respect to const? For example this is allowed in C++, but wrong in C: // Foo.cpp const int Foo = 99; // Main.cpp const int Foo = 99; int main() { cout << Foo