85 questions
2
votes
1
answer
126
views
Is it legal to declare a function template of type which has C language linkage?
Function templates are not allowed to have C language linkage:
extern "C" {
template<typename T> // error: template with C linkage
void bad_f() {}
}
This is reasonable, as ...
5
votes
1
answer
148
views
What's the difference between `extern "C" /*...*/` and `extern "C" { /*...*/ }`
Both GCC and Clang reject
extern "C" static void foo() {}
but accept
extern "C" {
static void foo() {}
}
Aren't these supposed to be equivalent?
-2
votes
1
answer
147
views
Can I write a library to preload in C++? Is there anything I need to do other than prepend `extern "C"` to the functions to intercept? [closed]
I am working on a personal project where I need to intercept Linux APIs like open(), read() etc, and I would like to do some data analysis on them. I would need to keep a C++ data structure std::map ...
0
votes
4
answers
290
views
Can I detect (at compile time) whether I'm in an extern "C" {} block?
Say I have a C header file a.h
Any C++ header file, say a.hpp that #includes a.h, should do so in an extern "C" block.
So I have some header files that are common between our C++ code and C ...
0
votes
2
answers
176
views
C++ using extern C library, unexpected memory leak
I have created a simple C library is generated by one .c and one .h file.
// test.c
#include "test.h"
#include "stdlib.h"
void GetArray(int * array)
{
array = (int *) calloc(2, ...
-1
votes
1
answer
587
views
C++ Shared Library does not export "extern C" functions [closed]
I'm working on a shared library and adding some functions which I export as c functions, so I can use them easily in C#. But none of my c functions are exported.
According to the app DependenciesGUI ...
1
vote
1
answer
163
views
extern "C": Why does Visual C++ behave different on conflicting linkage specs for variables vs. for functions?
If a variable is declared somewhere without extern "C" (e.g. in a header file) and afterwards defined with extern "C", then the Visual C++ compiler compiles it with C++ linkage ...
2
votes
2
answers
133
views
If I declare a function with extern "C", should I also define it that way?
In my header file, foo.h, I have:
#ifdef __cplusplus
extern "C" {
#endif
int foo(int x);
#ifdef __cplusplus
}
#endif
Now, in foo.cpp, should I also use extern "C", and define:
#...
-1
votes
1
answer
90
views
Calling a C file in C++ is giving errors
The reproduceable error code is:
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//#include <...
0
votes
1
answer
92
views
Including extern c header results in broken preprocessor output
I have a source code structured as follows
foo.h (header from c library)
#pragma once
struct foo {...};
bar.h
#pragma once
extern "C" {
#include "foo.h"
}
class Bar {
foo val;...
2
votes
1
answer
228
views
extern c template instantiation
I want to write a templated function and explicitly instantiate it inside extern "C" block to avoid code duplication.
Here is example of what I mean:
template<typename T> // my ...
3
votes
2
answers
299
views
One-liner extern "C" for a variable - legit?
Consider the following translation unit:
extern "C" int x = 1;
I know that C-mangling an int doesn't mean much; and that int x = 1 already guarantees external linkage, but this should work. ...
0
votes
0
answers
182
views
How can I check whether a function is extern-C?
In C++, can I check whether a certain function has been declared "extern C" or not?
I don't mind checks restricted to the current translation unit.
I'd like to check this at compile-time, ...
2
votes
3
answers
1k
views
Using a enum class from a c++ header in a c header
I am writing a c wrapper around a c++ library.
In the c++ there are enum classes used as types for function arguments.
How do I use theme correctly in the c header.
One ugly way would be to use int's ...
7
votes
3
answers
3k
views
extern "C" - before or after library header includes?
I'm writing a C library, which may potentially be useful to people writing C++. It has a header which looks like this:
#ifndef FOO_H_
#define FOO_H_
#include <bar.h>
#include <stdarg.h>
#...
0
votes
1
answer
161
views
How common is it to mix C with C++? [closed]
I wanted to know how widely used is the mixing of C++ and C. I mean as in using of C libraries/functions and call it in C++ program like how it is done here Mix C with C++. How extensive is its use in ...
1
vote
0
answers
61
views
extern "C" function returning std::array [duplicate]
The following code compiles with gcc but does not compile with MSVC (Godbolt link)
#include <array>
extern "C" std::array<float,3> foo()
{
return std::array<float,3>{1,...
1
vote
1
answer
223
views
Resolution of overloaded extern C function with default arguments
This code:
#include <stdlib.h> // int abs(int);
int abs(int i = 0) { return 42; }
int main() {
return abs(1); // Returns 42
}
Returns 42.
The compiler picks the overloaded C++ function. I ...
7
votes
2
answers
264
views
Is it safe to "play" with parameter constness in extern "C" declarations?
Suppose I'm using some C library which has a function:
int foo(char* str);
and I know for a fact that foo() does not modify the memory pointed to by str. It's just poorly written and doesn't bother ...
0
votes
0
answers
1k
views
c++ LoadLibrary and GetProcAddress from extern DLL
I´ve got an extern DLL to work with. I try to load the DLL with LoadLibrary and get the function by calling GetProcAddress but everytime I debug the programm I got an exception, which I can ignore by ...
2
votes
1
answer
320
views
Using lambda for callback function as C function parameter
I was writing my own std::thread-like wrapper for pthread_* functions (for educational purposes). The constructor I came up with looked like this:
template<class Fn, class... Args>
explicit ...
3
votes
1
answer
865
views
Extern used twice in C++
I am very curious about what happens during linking, and, during my research in this area I have stabbed upon this code
#ifdef __cplusplus
extern “C” {
#endif
extern double reciprocal (int i);
#...
0
votes
2
answers
153
views
How do I hand over a pointer to a non-static member method to an extern "C" function? [duplicate]
I've got a class named tmc, which now contains (among other things, which are not relevant here) a private constructor, a static factory method, and an instance method named ReadLoop (void*):
extern "...
4
votes
4
answers
3k
views
Problem with using C code in C++ with extern "C"
I know when i want to link C code as C code in C++ should i use extern "C". But with the following code :
/* file.h */
some (void)
{
return 10;
}
extern "C"
{
#include "file.h"
}
#include <...
4
votes
1
answer
435
views
Are extern extern "C", and extern "C" extern, allowed?
Is this code correct?
extern "C" extern int x; // 1
extern extern "C" int y; // 2
extern "C" extern "C" int z; // 3
int main() { }
gcc rejects 1 and 2 as syntax errors and accepts 3. ...
7
votes
1
answer
965
views
"C linkage function cannot return C++ class" in Visual Studio 2019
I have the following function:
class Foo;
template <typename T>
struct PyArray1D{
std::size_t size;
T *array;
};
extern "C" PyArray1D<Foo> SimulatePhotonEvents()
{
Foo * ...
3
votes
1
answer
883
views
Returning unique_ptr from a function executed via dlsym
I have a function that is located in a shared object, and is loaded and executed with dlsym from the main program.
(both the shared object and the main program are C++)
Is it possible that this ...
0
votes
2
answers
73
views
Passing a pointer to a function as an argument to a function
Just wondering if anyone can give me some advice regarding where I'm going wrong here. My program works OK if I run it as is, but as soon as I swap the commented line with the one below it, I get ...
3
votes
2
answers
1k
views
To extern "C" or Not to extern "C" [g++ vs cl]
I'm comparing Numerical Recipes four1.c to Nayuki's FFT. Both are the C versions, but I'm using a C++ driver. For the comparison, I'm compiling (or more appropriately, linking) both into an executable ...
3
votes
2
answers
236
views
Cannot Call C++ Code from C without Error [duplicate]
I'm trying to write a C++ library that can be called from C. However, whenever I try to even write a bare minimum example, it crashes with undefined references. Here is my code:
mylibrary.h
#ifndef ...
0
votes
0
answers
56
views
Calling C++ code from C using inbuilt c++ classes and templates
So I want to use string class and multiset template of C++.
My original code interfaces from Python to C using ctypes and now I am trying to interface C to C++. (If there is any direct interfacing ...
0
votes
1
answer
181
views
Check whether function called through function-pointer has a return statement
We have a plugin system that calls functions in dlls (user-generated plugins) by dlopening/LoadLibrarying the dll/so/dylib and then dlsyming/GetProcAddressing the function, and then storing that ...
0
votes
1
answer
495
views
VS2013 C++ Compiler Mangling name defined with extern "C"
I'm trying to build a WIN32 console app that uses the current 2.12.28 ftd2xx.lib static library from FTDI. I'm using VS2013 and native unmanaged C++. My call looks like this.
#include "../ftd2xx.h"
...
4
votes
1
answer
1k
views
Link to Fortran library (Lapack) from C++
I'm using Lapack in my C++ code. I'm quite confused how to properly link to the library. Here is a small example corresponding to my code calling a function from Lapack:
#include <iostream>
...
2
votes
5
answers
786
views
Unable to call C++ function from a C code
I'm new to mixing C & C++ code. Understood the need of extern & __cplusplus directives after reading some SO links & online reading.
Not sure why am I getting the error. Did i miss ...
4
votes
0
answers
400
views
Generic lambda in extern "C" function
Following code is accepted by gcc 6 and clang 4, but MSVC 2017 which claims to support C++14 (and generic lambdas in particular) discards it with error C2894: templates cannot be declared to have 'C' ...
0
votes
2
answers
98
views
Should you use extern "C" in a file that only has defines?
Is it pointless to have:
#ifdef __cplusplus
extern "C" {
#endif
// code...
#ifdef __cplusplus
}
#endif
Where "code..." is just a bunch of defines and typedefs (no includes, etc.)?
0
votes
0
answers
332
views
Can't push another viewcontroller when passing data from unity3d to iOS
I have a bridge between unity 3d and iOS to pass data. I can sent data successfully from unity 3d to my iOS using extern "C", then i can call a objective c method from a method of extern "c". But from ...
1
vote
2
answers
2k
views
Is clang++ ignoring extern "C" for some deprecation warnings?
If I use clang 3.8.1 to compile:
extern "C" {
int foo(int x) { register int y = x; return y; }
}
int main() { return foo(123); }
I get the warning:
a.cpp:3:18: warning: 'register' storage ...
-1
votes
1
answer
506
views
Unresolved external class member when linking static lib from dynamic dll
MS Visual Studio 2008. This seems to be a name mangling issue, but I can't find the right search terms to come up with an answer.
I have a dynamic lib that has a class in it, which is using a logging ...
2
votes
2
answers
121
views
Can I have a pointer on pointer on const in C++?
Basically I am wondering whether something like
f(const void* a){
// This is also what `(char**) a` would do behind the scenes
char** string_a_ptr {reinterpret_cast<char**>(const_cast<...
2
votes
0
answers
521
views
linking to C++ libraries to Modelica
I'm trying to utilize a C++ library in Modelica. Modelica compilers generate c from Modelica source and then invokes a c compiler to create an executable. Modelica provides a mechanism to call c ...
4
votes
1
answer
455
views
Does it make sense to use _attribute__ ((nothrow)) inside extern C?
I have some C code being called from C++.
The header resembles the following:
#ifndef CLibH
#define CLibH
#ifdef __cplusplus
extern "C" {
#endif
//C API
void foo(void);
// ...
#ifdef __cplusplus
...
11
votes
4
answers
11k
views
extern and extern "C" for variables
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
...
3
votes
2
answers
3k
views
Why does Visual Studio fail to give an undefined reference error when extern "C" is specified?
Given this code:
A2.H
_declspec(dllimport) void SomeFunc();
struct Foo
{
Foo();
~Foo();
};
inline Foo::Foo() { }
inline Foo::~Foo()
{
SomeFunc();
}
A1.H
#include "A2.h"
extern "C" ...
1
vote
1
answer
227
views
extern "C" functions in compiled object code
Environment is Microsoft Visual C++ 2015 and Windows 7.
Is there anything special about inline extern "C" functions defined in a header? I am consuming an SDK in which one of the headers contain such ...
0
votes
3
answers
606
views
How to restrict access to static variables in C++?
I have a C-function called "count" that looks like this:
void count(){
static int c = 0;
printf("Counter=%i", c);
c++;
}
Futhermore I have a vector of Cpp-objects and each object calls the "...
3
votes
0
answers
2k
views
extern c linkage errors in visual studio
I have a simple c++ Visual Studio project:
#include "test.h"
test::test()
{
}
test::~test()
{
}
HEADER:
#pragma once
#include <string>
class test
{
public:
test();
~test();
};
When I ...
7
votes
2
answers
848
views
What kinds of C++ functions can be placed in a C function pointer?
I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code.
extern "C" {
typedef struct callbacks_t {
void (*foo) (const char*);
int (*...
9
votes
4
answers
6k
views
how does extern "C" allow C++ code in a C file?
In order to use C++ code in a C file, I read that we can just do extern "C" { (where the c++ code goes here)}, but when I try printing something out using cout, I keep getting an error because it does ...