Calling C Functions from C++ Code

1. Introduction

Recently, I used C++ code to call functions from C code, and I would like to document the process.

2. Code

1. Include Header Files

The content of the include/add.h header file is as follows:

#ifndef __ADD_HPP__
#define __ADD_HPP__
#include <stdio.h> 
#include <stdlib.h>

int function_add(int a, int b);

#endif

The content of the include/function.hpp header file is as follows:

#ifndef __FUNCTION_H__
#define __FUNCTION_H__

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include "add.h"

int test(int a, int b);

#endif

2. Source Files

src/add.c

#include "add.h"

int function_add(int a, int b)
{
    return a + b;
}

src/function.cpp

#include "function.hpp"

int test(int a, int b)
{
    return function_add(a, b);
}

src/main.cpp

#include "function.hpp"

int main()
{
    int a = 1;
    int b = 2;
    int out;
    out = test(a, b);
    std::cout << "The result is: " << out << std::endl;
    return 0;
}

3. CMakeLists.txt Content

cmake_minimum_required(VERSION 3.10)
project(main)
set(CMAKE_CXX_STANDARD 20)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -Wextra -Wpedantic")

include_directories(
    ${PROJECT_SOURCE_DIR}/include
)

add_executable(
    ${PROJECT_NAME}
    src/add.c
    src/function.cpp 
    src/main.cpp
)

target_link_libraries(${PROJECT_NAME} pthread)

4. Compilation Error

The error message is as follows:

/usr/bin/ld: CMakeFiles/main.dir/src/function.cpp.o: in function `test(int, int)':
function.cpp:(.text+0x5): undefined reference to `function_add(int, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:114: ../bin/main] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
Calling C Functions from C++ Code
Insert image description here

Looking at the error, I thought there might be an issue with my CMakeLists.txt that caused the add.h header file not to be included. After reviewing the code, I found no issues. Upon further investigation, I discovered that there was a problem with the add.h file. To allow C++ to call it, the header file declaration needed to be modified.

5. Modifying the C Header File

src/add.h

#ifndef __ADD_HPP__
#define __ADD_HPP__
#include <stdio.h> 
#include <stdlib.h>

#ifdef __cplusplus
    extern "C" {
#endif

int function_add(int a, int b);

#ifdef __cplusplus
}
#endif

#endif

After modification, the compilation proceeded without issues, and the program ran successfully.

6. Key Parts Analysis

#ifdef __cplusplus
This is a predefined macro for the C++ compiler; this condition is true only in a C++ compilation environment.
In a C compilation environment, this condition is false.
extern "C" Note that the C is uppercase.
Its purpose is to inform the C++ compiler to handle function names in a C language manner.
Without extern "C", C++ code cannot find functions compiled in C.

7. Conclusion

I learned how to call C functions from C++ code. The method described above is just one of many; I will write about other methods in the future.

Leave a Comment