linkage

extern and extern “C” for variables

不羁岁月 提交于 2019-12-04 08:49:16
I'm writing a C++ shared library for a C program to use. However, I have a question about extern and extern "C" . Consider the following code My header file is like this: #ifdef __cplusplus extern "C" int global; extern "C" int addnumbers(int a, int b); #else extern int global; #endif This works perfectly fine; I just have to declare int global; in either my .cpp or my .c file. However, what I don't understand is: What is the difference between extern "C" and extern here? I tried commenting out extern "C" int global and it works! Why? I know that extern "C" is used for making C linkage. That's

Is it possible to overload functions with extern linkage?

风格不统一 提交于 2019-12-04 07:15:46
I saw a strange code, in declaration of std::atexit : extern "C" int atexit( void (*func)() ); extern "C++" int atexit( void (*func)() ); // ... why are there two functions? I guess it's some kind of function overloading, but it seems obviously wrong. What's it? and why is it necessary? Lightness Races with Monica The problem with your source This is cppreference being a little misleading. Declaring the same function twice with different storage-class-specifiers is illegal and causes a build failure . If you look at the source for libstdc++ (GCC's standard library implementation), you'll see

No linkage at block scope?

ぃ、小莉子 提交于 2019-12-04 01:55:32
Do all variables declared in a block have 'no linkage'? For example: 1: If I declare a static variable: void foo() { static int i; } Would it have an internal linkage or no linkage? If no linkage, then why make it static? 2: What happens if I use extern? /*global scope*/ static int i; void foo() { extern int i; } In this case, what will be the linkage of i ? Indeed, 'no linkage' at function scope. The goal is lifetime management: the static has the lifetime of a global static, while it has the visibility (scope) of a local. Note In C++ you can also declare statics ('globals') without linkage

undefined reference to c functions in c++ code

柔情痞子 提交于 2019-12-03 21:33:47
I have a strange problem: the code bellow perfectly compiled. src.cpp: extern "C" { #include "header.h" } void A::Execute() { B::Instance().Reset(ix); c_func(ix);// this is c functions declared in header.h C::Instance().Erase(ix); } But when I comment out the c_funk() I get linkage error in all places where I use c functions from the header.h file. With that minor change: void A::Execute() { B::Instance().Reset(ix); //c_func(ix);// this is c function declared in header.h C::Instance().Erase(ix); } I get: undefined reference to c_func(). Any ideas how to solve it? Thanks. Update: I have added a

R - simple Record Linkage - the next step ?

拈花ヽ惹草 提交于 2019-12-03 20:08:25
I am trying to do some simple direct linkage with the library('RecordLinkage') . So I only have one vector tv3 = c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE", "TOURDE FRANZ", "GET FRESH") The function that I need is compare.dedup of the library('RecordLinkage') and I get : compare.dedup(as.data.frame(tv3))$pairs $pairs id1 id2 tv3 is_match 1 1 2 1 NA 2 1 3 0 NA 3 1 4 0 NA 4 1 5 0 NA 5 2 3 0 NA .... I have trouble finding documentation for the next step. How do I then compare and find my similar pair ? So I found the distance jarowinkler() but it returns only pairs. Basically, you can only

Using Three20 with another library and conflicting linkage flags

落爺英雄遲暮 提交于 2019-12-03 17:37:30
I'm trying to add Three20 to my project, but the -ObjC and -all_load flags are messing with another library I'm using. The other library is ZXingWidget for barcode reading, but I don't think that part is relevant. I'm reasonably sure the answer is to use force_load instead of all_load and then point to my three20 libraries, but I can't get it to work. Here's what i'm using now: -force_load ../facebook-three20/Build/Products/Debug-iphonesimulator/*.a But I get an errno=22 build fail immediately. Even if I get the force_load to work, the -ObjC flag causes issues all by itself. How am I supposed

Difference between constexpr and static constexpr global variable

淺唱寂寞╮ 提交于 2019-12-03 15:44:15
问题 In the C++11 standard, what is the difference between constexpr and static constexpr global variables when defined in a header? More specifically, when multiple translation units include the same header, which declaration (if any) is guaranteed to define the same variable across the translation units? e.g., cexpr.h: #ifndef CEXPR_H #define CEXPR_H constexpr int cint = 1; static constexpr int scint = 1; #endif a.cpp: #include "cexpr.h" b.cpp: #include "cexpr.h" 回答1: In your current example

About inconsistent dll linkage

不羁的心 提交于 2019-12-03 14:23:20
问题 How can I remove this link warning? You can see code segment that causes this warning. static AFX_EXTENSION_MODULE GuiCtrlsDLL = { NULL, NULL }; //bla bla // Exported DLL initialization is run in context of running application extern "C" void WINAPI InitGuiCtrlsDLL() { // create a new CDynLinkLibrary for this app new CDynLinkLibrary(GuiCtrlsDLL); // nothing more to do } warning C4273: 'InitGuiCtrlsDLL' : inconsisten t dll linkage I have also export and import definitions, like: #ifdef

extern enum in c++

和自甴很熟 提交于 2019-12-03 13:05:41
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? 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 the size of a data pointer, no matter what type it's pointing to. One of the things you can't do with an

What does the standard say about char arrays as template arguments?

时间秒杀一切 提交于 2019-12-03 11:18:58
During my research for an answer for this question I found (I did not know that before) that gcc and clang allow char arrays to be template arguments if they are declared static . E.g. this code compiles with gcc and clang: #include <type_traits> template <int N, const char (&string)[N]> auto foo() { if constexpr (string[0] == 'i') return 0; else return 3.14f; } void bar() { static constexpr char string1[] = "int"; static constexpr char string2[] = "float"; auto i = foo<sizeof(string1), string1>(); auto f = foo<sizeof(string2), string2>(); static_assert(std::is_same_v<decltype(i), int>);