在c代码中使用c ++编写的函数

Abh*_*ngh 6 c c++ linux gcc

我正在开发一个项目,其中一些人已经用C++编写代码,我们必须在C代码中使用它.所以我尝试按照测试编写一个测试程序来演示相同的:

头文件是:

 #ifndef h_files_n
    #define h_files_n

    #include<iostream>

    #ifdef __cplusplus
        extern "C" {
    #endif

    void add_func(int, int);

    #ifdef __cplusplus
        }
    #endif

    #endif
Run Code Online (Sandbox Code Playgroud)

cpp文件是:

#include"h_files.h"

void add_func(int num, int nums)
{
    std :: cout << "The addition of numbers is : "<< num+nums << std endl;
}
Run Code Online (Sandbox Code Playgroud)

c文件是:

#include<stdio.h>
#include"h_files.h"

int main()
{
    printf("We are calling the C function form C++ file.\n");

    add_func(10,15);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

makefile是:

CC = gcc
inc = -I include

vpath %.c src
vpath %.cpp src
vpath %.o obj

all : $(addprefix obj/,functions.o main.o) run

run : main.o functions.o
    $(CC) -o bin/$@ $^

obj/%.o : %.cpp
    $(CXX) -c $(inc) $^ -o $@ -libstdc++

obj/%.o : %.c
    $(CC) -c $(inc) $^ -o $@

.PHONY : clean

clean :
    -rm -f obj/* bin/*
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

g++ -c -I include src/functions.cpp -o obj/functions.o
gcc -c -I include src/main.c -o obj/main.o
gcc -o bin/run obj/main.o obj/functions.o
obj/functions.o: In function `add_func':
functions.cpp:(.text+0x1e): undefined reference to `std::cout'
functions.cpp:(.text+0x23): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
functions.cpp:(.text+0x2d): undefined reference to `std::ostream::operator<<(int)'
functions.cpp:(.text+0x32): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
functions.cpp:(.text+0x3a): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
obj/functions.o: In function `__static_initialization_and_destruction_0(int, int)':
functions.cpp:(.text+0x68): undefined reference to `std::ios_base::Init::Init()'
functions.cpp:(.text+0x77): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
make: *** [run] Error 1
Run Code Online (Sandbox Code Playgroud)
  • 如果我使用g ++作为链接器,那么它工作正常.但是gcc链接器给了我这个问题.
  • 我想到的问题是g ++的函数名称变形,而gcc不会破坏函数名称(符号).
  • 那么有没有任何编译器标志可以避免名称损坏,如果可能的话.
  • 避免g ++链接器的原因是它将链接视为C++样式,但基本代码在C中,因此它可能会在调用代码时引入一些错误.

请有人建议解决方案.

πάν*_*ῥεῖ 10

$(CC) -o bin/$@ $^

使用

run : main.o functions.o
    $(CXX) -o bin/$@ $^
Run Code Online (Sandbox Code Playgroud)

而是使用g++链接器将所有内容链接在一起,并libstc++.a自动设置链接库的默认值.

否则,您需要-lstc++明确指定其他库.


此外,如果您正在为您的c ++代码设计纯c-API,那么您应该拥有

 #include <iostream>
Run Code Online (Sandbox Code Playgroud)

functions.cpp仅在源文件中的语句.从中删除c ++标准头文件functions.h.


请不要using namespace std;在c ++头文件中使用.它很容易调用所有意外类型的命名空间冲突(没有人知道所使用的所有名称namespace std).

它无论如何都不应出现在纯c-API头文件中.


如果您想要一个混合的 c/c ++ API头文件,请将所有 c ++特定语句放在c ++保护中:

 #ifdef __cplusplus
 #include<iostream>

 class xyz; // A c++ forward declaration
 #endif
Run Code Online (Sandbox Code Playgroud)


Sla*_*ica 6

您应该链接程序g++或手动指定libstdc ++库(ies).