linkage

Easy way to guarantee binary compatibility for C++ library, C linkage?

二次信任 提交于 2019-11-30 00:08:12
问题 I don't know exact way to guarantee binary compatibility for C++ library on both Windows and Linux. But I think if I make all exposed APIs with a C linkage I think I can easily guarantee this compatibility on both Windows and Linux. It's like below : extern "C" int SomeAPI(); Rest of the functions surely will be written in C++. Only the APIs exposed to outter world will have C linkage. And the exposed API's must be pure C function - no exceptions, no C++ types are used. Am I right or wrong?

Linkage of various const/static variables

自古美人都是妖i 提交于 2019-11-29 23:22:32
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 int b; // (c) static int b; // (d) invalid! First, (c) implies external linkage. But (d) implies

What are ld-linux.so.2 and linux-gate.so.1?

亡梦爱人 提交于 2019-11-29 22:58:00
When I run ldd program I get an output of the form linux-gate.so.1 => (0xb77ae000) libstdc++.so.6 => /lib/libstdc++.so.6 (0xb76bc000) libm.so.6 => /lib/libm.so.6 (0xb7691000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7674000) libc.so.6 => /lib/libc.so.6 (0xb74c2000) /lib/ld-linux.so.2 (0xb77af000) Can you explain the output and the reason linux-gate.so.1 and ld-linux.so.2 show differently than other entries? What are their roles? Petesh I hope that you're not asking about the main entries, which are stating that for the requested library libm.so.6 it was found in the file /lib/libm.so.6 , for

Why do inline functions have external linkage by default?

梦想与她 提交于 2019-11-29 06:10:19
问题 The standard says that given a declaration of inline void foo(); that foo is an inline function with external linkage (because by default all function declarations have external linkage). This strikes me as odd. because the one definition rule section 3.2 (in both C++03 and C++11) say: 3 ... An inline function shall be defined in every translation unit in which it is used. 5 There can be more than one definition of a[n] ... inline function with external linkage (7.1.2) ... Given such an

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

给你一囗甜甜゛ 提交于 2019-11-29 05:44:58
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 is neither explicitly declared extern nor previously declared to have external linkage [...] But I don

Determining C executable name

浪尽此生 提交于 2019-11-29 02:09:54
问题 When we are compiling a C program the output is stored in a.out. How can we redirect the compiled output to another file? 回答1: Most C compilers provide the -o option for this, such as: gcc -o gentext gentext.c cc -o mainprog -Llib -lmymath firstbit.c secondbit.o xlc -o coredump coredump.c 回答2: -ofilename will make filename instead of a.out . 回答3: According to the manual: -o <file> Place the output into <file> 回答4: In Unix, where C originated from, C programs are usually compiled module-by

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

Linkage of symbols within anonymous namespace within a regular namespace

本小妞迷上赌 提交于 2019-11-28 20:42:35
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() { // still internal linkage? } } } legends2k C++11 (draft N3337) §3.5/4: (emphasis mine) An unnamed

boost libraries built with relative paths

て烟熏妆下的殇ゞ 提交于 2019-11-28 12:58:52
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-multi/future.o" "bin.v2/libs/system/build/qcc/release/threading-multi/libboost_system.so.1.57.0" -lm So,