undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif

最近在弄一个进程间通信,原始测试demon用c语言写的,经过测试ok,然后把接口封装起来了一个send,一个recv。

使用的时候send端是在一个c语言写的http服务端使用,编译ok没有报错,但是recv的使用在QT里面是C++的,编译的时候出现

undefined reference to `recvIpcMsg(int, ipc_msg*)‘

报错。

检查了头文件和实现文件都在,编译成动态库了都,同样的方法在C文件里调用都没有报错,搬到C++里面就都报错了,突然想起来一件事我的头文件声明没有对C++编译器专门处理。

如下:

/**ipcmsg.h

*/
#ifndef H_MSGIPC_H
#define H_MSGIPC_H

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MSGKEY 8888
#define BUF_SIZE 64 

typedef enum ipc_msg_type
{
        APP_MSG,
        INPUT_MSG,
        GPS_MSG
}ipc_type;

typedef struct msgIpc
{
   ipc_type type;
   char buf[BUF_SIZE];
}ipc_msg_t;
//send ipc msg
extern int sendIpcMsg(key_t msgkey, const ipc_msg_t *msg);
//recv ipc msg
extern int recvIpcMsg(key_t msgkey, ipc_msg_t *msg);
#endif

如果C的方法要在C++编译器里链接使用应该加上如下宏包含

#ifdef __cplusplusextern "C" {
#endif

#ifdef __cplusplus
}
#endif

如此才能正常编译链接。

最后修改如下就可以编过了

/*
*ipcmsg.h
*/
#ifndef H_MSGIPC_H
#define H_MSGIPC_H
#ifdef __cplusplusextern "C" {
#endif

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

 

typedef enum ipc_msg_type
{
        APP_MSG,
        INPUT_MSG,
        GPS_MSG
}ipc_type;

typedef struct msgIpc
{
   ipc_type type;
   char buf[BUF_SIZE];
}ipc_msg_t;
//send ipc msg
extern int sendIpcMsg(key_t msgkey, const ipc_msg_t *msg);
//recv ipc msg
extern int recvIpcMsg(key_t msgkey, ipc_msg_t *msg);
 
#ifdef __cplusplus
}
#endif
#endif

undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif

原文地址:https://www.cnblogs.com/tid-think/p/9272218.html

时间: 2024-08-06 19:57:23

undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif的相关文章

C++ 为什么要使用#ifdef __cplusplus extern &quot;C&quot; { #endif

转载:http://www.cnblogs.com/ayanmw/archive/2012/03/15/2398593.html 转载:http://blog.csdn.net/zkl99999/article/details/48134621 转载: http://www.jianshu.com/p/5d2eeeb93590 经常看到别人的头文件 有这样的代码 #ifdef __cplusplus extern "C" { #endif // C 样式 的函数 #ifdef __cp

#ifdef __cplusplus extern &quot;C&quot; { #endif”的定义

看一些程序的时候老是有 "#ifdef __cplusplus extern "C" { #endif"的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefined Macros __cplusplus Defined for C++ programs only. 意思是说,如果是C++程序,就使用 extern "C"{ 而这个东东,是指在下面的函数不使用的C++的名字修饰,而是用C的 The following c

“#ifdef __cplusplus extern &quot;C&quot; { #endif”的定义

平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #endif // 代码 #ifdef __cplusplus } #endif 这个是什么意思呢?一开始看到这个也很茫然.上网查找了一些资料. 主要作用: 为了在C++代码中调用用C写成的库文件,就需要用extern"C"来告诉编译器:这是一个用C写成的库文件,请用C的方式来链接它们. 原因:

#ifdef __cplusplus extern &quot;C&quot; { #endif

1.在好多程序中我们会遇到下面代码段 #ifdef __cplusplus        extern "C" {        #endif //c语法代码段 #ifdef __cplusplus        }        #endif // 首先应该知道,__cplusplus是CPP中的自定义宏,则表示这是一段cpp的代码,编译器按c++的方式编译系统..如果这时候我们需要使用c语言的代码,那么就需要加上(extern "C" { )这一段来说明,要不编

#ifdef __cplusplus extern &quot;C&quot; { #endif 的意思(转)

Microsoft-Specific Predefined Macros__cplusplus Defined for C++ programs only. 意思是说,如果是C++程序,就使用extern "C"{而这个东东,是指在下面的函数不使用的C++的名字修饰,而是用C的 The following code shows a header file which can be used by C and C++ client applications:// MyCFuncs.h#i

#ifdef __cplusplus extern &quot;C&quot; { #endif //一段代码 #ifdef __cplusplus } #endif

这样的代码到底是什么意思呢?首先,__cplusplus是cpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入"extern "C"{" 和 " }"处理其中的代码,其中{ }内部的代码是通过extern"C"进行处理.要明白为何使用extern"C",还得从cpp中对函数的重载处理开始说起.在c++中,为了支持重载机制,在编译生

#ifdef __cplusplus extern C{}与C和C++间的关系

#ifdef __cplusplus extern C{}与C和C++间的关系 1.1 问题是什么 解决在 1.在一个系统中.cpp文件的函数需要调用.c文件的函数,及需要gcc和g++编译的文件或文件的部分函数在同一个头文件中. 的情况下,如何正确的编译.链接. 1.2 基础知识 Compile the C code like this:     gcc -c -o somecode.o somecode.c Then the C++ code like this:     g++ -c -o

【转】#ifdef __cplusplus+extern &quot;C&quot;的用法

时常看到别人的头文件中,有这样的代码: 1 #ifdef __cplusplus 2 3 extern "C" { 4 5 #endif 6 7 //一段代码 8 9 #ifdef __cplusplus 10 11 } 12 13 #endif 这样的代码到底是什么意思呢?首先,__cplusplus是cpp中的自定义宏,那么定义了这个宏的话表示这是一段cpp的代码,也就是说,上面的代码的含义是:如果这是一段cpp的代码,那么加入extern "C"{,和 }处理

undefined reference to 问题汇总及解决方法 ----- 还有一种问题没有解决(可能是顺序问题)

1.链接时缺失了相关的目标文件 2.链接时缺少了相关的库文件 3.链接的库文件中有使用了另一个库文件 4.多个库文件链接顺序问题 5.定义与实现不一致 6.在c++代码中链接C语言的库 转载地址: https://segmentfault.com/a/1190000006049907?utm_source=tuicool&utm_medium=referral 在实际编译代码的过程中,我们经常会遇到"undefined reference to"的问题,简单的可以轻易地解决,但