随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

undefined reference to `add(int, int)'

一、概述

  使用cmake编译C++项目报

 undefined reference to `add(int, int)'

  项目中有.cpp文件,有.h文件,有.c文件。也就是c和c++混编。其中.cpp文件引用了.c的头文件

  现象:

   从上面可以看出其实这几个.cpp和.c文件都已经编译过了。但是就会提示找不到.c文件中的方法

二、解决办法

  原因在于.c和.cpp文件混编的时候需要在.h文件中加入一个判断,如果是c++环境则需要加上extern "C"。如下:

复制代码
#ifndef _HEAD_H
#define _HEAD_H

#ifdef __cplusplus
extern "C"{
#endif

//加法
int add(int a,int b);
//减法
int subtract(int a,int b);
//乘法
int multiply(int a,int b);
//除法
double divide(int a,int b);

#ifdef __cplusplus
}
#endif

#endif
复制代码

 

  

posted on   飘杨......  阅读(154)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2013-11-08 android java.lang.ExceptionInInitializerError
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示