calling-convention

Calling C code from C++ in a CMake project. Undefined symbol. Have extern C

£可爱£侵袭症+ 提交于 2020-07-16 10:29:47
问题 I'm trying to build a CMake project that calls C code from C++, and I'm getting undefined symbols, even though I'm (AFAIK) properly using "extern C". CMakeLists.txt: cmake_minimum_required(VERSION 3.0) project(CTest LANGUAGES CXX) add_executable(test main.cpp lib.c) main.cpp: #include "lib.h" int main() { printit(); return 0; } lib.c: #include <stdio.h> #include "lib.h" int printit() { printf("Hello world\n"); return 0; } lib.h: extern "C" int printit(); That gives me an "undefined reference

What kinds of C++ functions can be placed in a C function pointer?

∥☆過路亽.° 提交于 2020-06-11 22:33:01
问题 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 (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

不羁岁月 提交于 2020-06-11 22:32:34
问题 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 (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

南楼画角 提交于 2020-06-11 22:32:30
问题 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 (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

匆匆过客 提交于 2020-06-11 22:32:11
问题 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 (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

Why is 0 moved to stack when using return value?

假如想象 提交于 2020-05-22 06:46:41
问题 I'm experimenting disassembling clang binaries of simple C programs (compiled with -O0 ), and I'm confused about a certain instruction that gets generated. Here are two empty main functions with standard arguments, one of which returns value and other does not: // return_void.c void main(int argc, char** argv) { } // return_0.c int main(int argc, char** argv) { return 0; } Now, when I disassemble their assemblies, they look reasonably different, but there's one line that I don't understand:

Why does this REPNE SCASB implementation of strlen work?

房东的猫 提交于 2020-05-09 07:41:06
问题 Why does this code work? http://www.int80h.org/strlen/ says that the string address has to be in EDI register for scasb to work, but this assembly function doesn't seem to do this. Assembly code for mystrlen : global mystrlen mystrlen: sub ecx, ecx not ecx sub al, al cld repne scasb neg ecx dec ecx dec ecx mov eax, ecx ret C main: int mystrlen(const char *); int main() { return (mystrlen("1234")); } Compilation: nasm -f elf64 test.asm gcc -c main.c gcc main.o test.o Output: ./a.out echo $? 4

Purpose of rdi register for no argument function

别来无恙 提交于 2020-03-04 05:03:40
问题 Consider this simple function: struct Foo { int a; int b; int c; int d; int e; int f; }; Foo foo() { Foo f; f.a = 1; f.b = 2; f.c = 3; f.d = 4; f.e = 5; f.f = 6; return f; } It generates the following assembly: 0000000000400500 <foo()>: 400500: 48 ba 01 00 00 00 02 movabs rdx,0x200000001 400507: 00 00 00 40050a: 48 b9 03 00 00 00 04 movabs rcx,0x400000003 400511: 00 00 00 400514: 48 be 05 00 00 00 06 movabs rsi,0x600000005 40051b: 00 00 00 40051e: 48 89 17 mov QWORD PTR [rdi],rdx 400521: 48