linkage

Internal linkage with static keyword in C

元气小坏坏 提交于 2019-11-27 22:19:59
I know static is an overloaded keyword in C. Here, I'm only interested in its use as a keyword to enforce internal linkage. If you have a global variable declared in a .c file, what is the difference between using static and not using static ? Either way, no other .c file has access to the variable, so the variable is basically "private" to the file, with or without the static keyword. For example, if I have a file foo.c , and I declare a global variable: int x = 5; That variable x is only available to code inside foo.c (unless of course I declare it in some shared header file with the extern

Can a variable be declared both static and extern?

假装没事ソ 提交于 2019-11-27 21:58:05
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? 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 linkage static int d ; // error: inconsistent linkage Section 3.5.6 discusses how extern should behave in this

constexpr global constants in a header file and odr

我与影子孤独终老i 提交于 2019-11-27 20:32:25
问题 Unfortunately, I am somewhat confused about constexpr , global constants declared in header files, and the odr. In short: Can we conclude from here https://isocpp.org/files/papers/n4147.pdf that constexpr MyClass const MyClassObj () { return MyClass {}; } constexpr char const * Hello () { return "Hello"; } is preferable over constexpr MyClass const kMyClassObj = MyClass {}; constexpr char const * kHello = "Hello"; for defining globals in a header file if I want to "just use" those globally

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

冷暖自知 提交于 2019-11-27 13:32:56
问题 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++? 回答1: It's not strictly legal. int a; is a tentative definition in C. You are allowed multiple

Linkage of symbols within anonymous namespace within a regular namespace

烂漫一生 提交于 2019-11-27 13:07:29
问题 In C++, putting a function or a variable in an anonymous namespace makes its linkage internal, i. e. the same as declaring it static on a file-level, but idiomatic C++. What about an anonymous namespace within a normal namespace? Does it still guarantee internal linkage? // foo.cpp void func1() { // external linkage } static void func2() { // internal linkage } namespace { void func3() { // internal linkage } } namespace ns1 { void func4() { // external linkage } namespace { void func3() { //

Why can't templates be within extern “C” blocks?

Deadly 提交于 2019-11-27 09:01:27
This is a follow-up question to an answer to Is it possible to typedef a pointer-to-extern-“C”-function type within a template? This code fails to compile with g++ , Visual C/C++, and Comeau C/C++ with basically the same error message: #include <cstdlib> extern "C" { static int do_stuff(int) { return 3; } template <typename return_t_, typename arg1_t_> struct test { static void foo(return_t_ (*)(arg1_t_)) { } }; } int main() { test<int, int>::foo(&do_stuff); return EXIT_SUCCESS; } g++ says "error: template with C linkage", Visual C/C++ emits compiler error C2894 , and Comeau C/C++ says "error:

boost libraries built with relative paths

笑着哭i 提交于 2019-11-27 07:17:36
问题 I built boost 1.57.0 in QNX 6.5.0. There is no build error. But some libraries are linked to libboost_system.so specifying relative path. I saved compilation logs. Here is linkage step for boost_thread: "QCC_gpp" -o "bin.v2/libs/thread/build/qcc/release/threading-multi/libboost_thread.so.1.57.0" -shared "bin.v2/libs/thread/build/qcc/release/threading-multi/pthread/thread.o" "bin.v2/libs/thread/build/qcc/release/threading-multi/pthread/once.o" "bin.v2/libs/thread/build/qcc/release/threading

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

房东的猫 提交于 2019-11-27 00:58:20
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)) proxy, ...); EDIT: Can you show a compiler or architecture where the static member version does not work

Internal linkage with static keyword in C

允我心安 提交于 2019-11-26 20:59:49
问题 I know static is an overloaded keyword in C. Here, I'm only interested in its use as a keyword to enforce internal linkage. If you have a global variable declared in a .c file, what is the difference between using static and not using static ? Either way, no other .c file has access to the variable, so the variable is basically "private" to the file, with or without the static keyword. For example, if I have a file foo.c , and I declare a global variable: int x = 5; That variable x is only

identify groups of linked episodes which chain together

橙三吉。 提交于 2019-11-26 14:47:52
Take this simple data frame of linked ids: test <- data.frame(id1=c(10,10,1,1,24,8),id2=c(1,36,24,45,300,11)) > test id1 id2 1 10 1 2 10 36 3 1 24 4 1 45 5 24 300 6 8 11 I now want to group together all the ids which link. By 'link', I mean follow through the chain of links so that all ids in one group are labelled together. A kind of branching structure. i.e: Group 1 10 --> 1, 1 --> (24,45) 24 --> 300 300 --> NULL 45 --> NULL 10 --> 36, 36 --> NULL, Final group members: 10,1,24,36,45,300 Group 2 8 --> 11 11 --> NULL Final group members: 8,11 Now I roughly know the logic I would want, but don