Search code examples
c++cextern-c

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 <cstring>

//functions

#define true 1
#define false 0



#ifdef _MSC_VER
// define POSIX function strndup if not available
char* strndup(const char* s, size_t n) {
    size_t len;
    for (len = 0; len < n && s[len]; len++)
        continue;
    char* ptr = malloc(len + 1);
    if (ptr) {
        memcpy(ptr, s, len);
        ptr[len] = '\0';
    }
    return ptr;
}
#endif

char** split(const char* str, const char* delimiters, int** a, int* size_of_a) {
    int i, count, len;
    char** final_result;
    const char* p;

    // phase 1: count the number of tokens
    p = str + strspn(str, delimiters);
    for (count = 0; *p; count++) {
        p += strcspn(p, delimiters);
        p += strspn(p, delimiters);
    }

    // phase 2: allocate the arrays
    final_result = calloc(sizeof(*final_result), count + 1);
    if (a) {
        *a = calloc(sizeof(**a), count);
    }
    if (size_of_a) {
        *size_of_a = count;
    }

    // phase 3: copy the tokens
    p = str;
    for (i = 0; i < count; i++) {
        p += strspn(p, delimiters);    // skip the delimiters
        len = strcspn(p, delimiters);  // count the token length
        if (a) {
            (*a)[i] = len;
        }
        final_result[i] = strndup(p, len); // duplicate the token
        p += len;
    }
    final_result[count] = 0;
    return final_result;
}


#ifdef __cplusplus
}
#endif

It started to give error:

Severity    Code    Description Project File    Line    Suppression State
Error   C2440   'initializing': cannot convert from 'void *' to 'char *'    example_win32_directx9  C:\Libraries\ImGui\imgui\examples\example_win32_directx9\Equation_simplifier.c  77  

How can this be fixed? I have set my compiler to C++14 and I am using visual studio 2019. I am using this in a non-main cpp file which is called in main cpp file. The main error I am getting from is malloc and calloc from what I have noticed. I am also getting error for getch().


Solution

  • It seems you want to write code in plain C and then have the split function callable from C++.

    Then you first of all need to make sure to build the source as plain C, which includes making sure the source file have a .c suffix, and not contain any C++ code (like the extern "C" part, or C++ header files like <cstring>).

    Once the file is built as plain C you create a header file, which use conditional compilation to use extern "C", for example one named my_split.h and looking something like this:

    #ifndef MY_SPLIT_H
    #define MY_SPLIT_H
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    char** split(const char* str, const char* delimiters, int** a, int* size_of_a);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif // MY_SPLIT_H
    

    Include that header file in your C++ code, and link with the object file generated from the C source file, and you should be able to use the split function.


    As mentioned, include the header file in the C source file as well, to make sure the function declarations match.