簡體   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