繁体   English   中英

从C函数调用时C ++函数的未定义引用

[英]Undefined reference for a C++ function when called from C function

我正在尝试从C函数调用C ++函数,但我看到了对函数编写的.cc文件的未定义引用,下面是代码。 我想念什么?

externcpp.cc

#include <iostream>
 #include "example.h"
 using namespace std;
 int main ()
 {
    cout << "I am " << __func__ << "In File " << __FILE__;
    return 0;
 }
 void example_fun()
 {
    cout << "I am" << __func__ << "in File __FILE__";
 }

外部

#include <stdio.h>
#include "example.h"
int test1()
{
    printf(" I am [%s] and from File [%s]\n",__func__,__FILE__);
    printf("Calling C++ Function from C\n");
    example_fun();
    return 0;
}

例子.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

#ifdef __cpluscplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

EXTERNC void example_fun();
#endif

并使用以下命令进行编译和链接

g++ -c -o externcpp.o externcpp.cc -Wall
gcc -c -o externc.o externc.c -Wall
g++ -o output externcpp.o externc.o

问候,

应该是#ifdef __cplusplus ,而不是上面代码中的#ifdef __cpluscplus 检查您的拼写。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM