1. 程式人生 > >undefined reference to `recvIpcMsg(int, ipc_msg*)'——#ifdef __cplusplus extern "C" { #endif

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

使用 type -s endif 但是 pcm nbsp rec c語言

最近在弄一個進程間通信,原始測試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 __cplusplus
extern
"C" { #endif #ifdef __cplusplus } #endif

如此才能正常編譯鏈接。

最後修改如下就可以編過了

/*
*ipcmsg.h
*/
#ifndef H_MSGIPC_H
#define H_MSGIPC_H

#ifdef __cplusplus
extern "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