linkage

Compiler error for conflicting variable declarations: “conflicts with new declaration with 'C' linkage”

独自空忆成欢 提交于 2019-12-11 03:03:00
问题 I ran across some legacy code that's failing to build on a newer compiler. The boiled down example: int x; extern "C" { int x }; // conflicts with C++ linkage above // note: without the braces it would've been equivalent to: // extern "C" { extern int x; } // // for reference, see the notes section here: // http://en.cppreference.com/w/cpp/language/language_linkage#notes The older compilers weren't flagging it, but both gcc (as of 4.1.2) and clang flag it. Clang's output: error: declaration

File scope data with C++ namespaces

点点圈 提交于 2019-12-11 02:16:27
问题 I've got some embedded C++ code that's currently written in a very C-like way, and I'd like to convert it to use namespaces for better code organization. Currently I hide my private file scope functions and variables in anonymous namespaces, but I'm not sure where to hide it using this new pattern. Should I still use an anonymous namespace, or is adding it to the namespace in the .cpp file, but not the header file sufficient enough to prevent external access? More specifically, I've got code

Calling a function with internal linkage via pointer from another translation unit

人走茶凉 提交于 2019-12-10 19:58:01
问题 Can we declare a callback function in an anonymous namespace (thus giving it internal linkage), knowing that it will be called from another translation unit (another library even)? Some lib: void register_callback(void (*cb)()) { .. cb(); .. } Main program namespace { int foo_cb() { ... } // internal linkage } int main() { register_callback(foo_cb); } 回答1: TL;DR: yes, it is ok From [basic.link] (emphasis mine): A name is said to have linkage when it might denote the same object, reference,

Interface to C++ objects through extern “C” functions

一世执手 提交于 2019-12-10 12:40:07
问题 Can an extern "C" function accept or return C++-specific data types, such as references, pointers-to-members, or non-POD classes (by value)? I cannot find anything in the C++ standard that forbids this. Logically, I would expect the standard to say something about it, as the C ABI is not necessarily suitable for passing such types around. The reason for me wanting to use C linkage has nothing to do with C compilers. The function is called only from C++ code. I just want to export unmangled

Symbol(s) not found for architecture x86_64 - Cmake - Mac sierra

对着背影说爱祢 提交于 2019-12-10 10:40:03
问题 Recently I have started a new project in C++. The problem is, when I try to compile it I get a linking error. I spent the whole day today trying to debug it, but I did not really find a good solution anywhere. If someone could help with it it would be amazing. I am using a Mac Sierra. parsing/methylation.h #ifndef EPIRL_METHYLATION_H #define EPIRL_METHYLATION_H #include <stdio.h> #include <iostream> #include <fstream> #include <vector> #include <sstream> using namespace std; namespace

Implicit function declarations and linkage

前提是你 提交于 2019-12-10 10:12:54
问题 Recently I've learnt about implicit function declarations in C . The main idea is clear but I have some troubles with understanding of the linkage process in this case. Consider the following code ( file a.c ): #include <stdio.h> int main() { double someValue = f(); printf("%f\n", someValue); return 0; } If I try to compile it: gcc -c a.c -std=c99 I see a warning about implicit declaration of function f() . If I try to compile and link: gcc a.c -std=c99 I have an undefined reference error. So

C and C++ linkage with extern “C”

▼魔方 西西 提交于 2019-12-10 06:39:55
问题 I have a C++ function defined in a .h file as follows and implemented in a .cpp file: extern "C" void func(bool first, float min, float* state[6], float* err[6][6]) { //uses vectors and classes and other C++ constructs } How can I call func in a C file? How do I set up my file architecture / makefile to compile this? Thanks! 回答1: To call it in C, all you need to do is call it normally. Because you told the compiler to use the C calling conventions and ABI with extern "C" , you can call it

Block scope linkage C standard

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 04:37:05
问题 The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern . { static int a; //no linkage } For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage,

C++/CLI->C# error C2526: C linkage function cannot return C++ class

百般思念 提交于 2019-12-10 02:26:40
问题 I have a simple .NET dll built with VS2010 C# that exposes 2 static members of a class public class Polygon { public static void Test(int test) {} public static void Test(List<int> test) {} } I then created a Console app from VS2010 C++ and added this function above _tmain extern "C" void TestMe() { Polygon::Test(3); } Adding the reference and compiling gives me this error 1>WierdError.cpp(9): error C2526: 'System::Collections::Generic::List<T>::GetEnumerator' : C linkage function cannot

R - simple Record Linkage - the next step ?

浪尽此生 提交于 2019-12-09 13:44:29
问题 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