大家好 QT c++调用c函数出错 可以帮忙看看解决下吗

qq_20835381 2018-04-21 10:35:20
这是我的C函数代码

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include "spidev.h"
#include <string.h>
#include "spi.h"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

void pabort(const char *s)
{
perror(s);
abort();
}

const char *device = "/dev/spidev4.0";
uint32_t mode;
uint8_t bits = 8;
unsigned int speed = 500000;
unsigned short int delay;

unsigned short int transfer(int fd,unsigned short int channel,unsigned short int delay)
{
int ret;
int i=0;
uint8_t tx[2]={0};
unsigned short int c=0;

//a=1<<5|a;
channel=channel<<4;
printf("channel=%x",channel);
tx[0]=channel>>8;
tx[1]=channel&0x00FF;

uint8_t rx[ARRAY_SIZE(tx)]={0,};
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long )tx,
.rx_buf = (unsigned long )rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
if (mode & SPI_TX_QUAD)
tr.tx_nbits = 4;
else if (mode & SPI_TX_DUAL)
tr.tx_nbits = 2;
if (mode & SPI_RX_QUAD)
tr.rx_nbits = 4;
else if (mode & SPI_RX_DUAL)
tr.rx_nbits = 2;
if (!(mode & SPI_LOOP)) {
if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
tr.rx_buf = 0;
else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
tr.tx_buf = 0;
}

ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
for(i=0;i<1000;i++);
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
pabort("can't send spi message");

for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
if (!(ret % 6))
puts("");
printf("%.2X ", rx[ret]);
// printf("%x",);
}
c=rx[0]<<8|rx[1];
printf("%x",c);
puts("");
return c;
}


void parse_opts()
{


mode |= SPI_CPOL;

if (mode & SPI_LOOP) {
if (mode & SPI_TX_DUAL)
mode |= SPI_RX_DUAL;
if (mode & SPI_TX_QUAD)
mode |= SPI_RX_QUAD;
}
}

int ADcollect(unsigned short int channel,unsigned int ADspeed,unsigned short int delay)
{
int ret = 0;
int fd;
unsigned short int d;
parse_opts();

if(ADspeed!=0)
{
speed=ADspeed;
}

fd = open(device, O_RDWR);
if (fd < 0)
pabort("can't open device");

/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
if (ret == -1)
pabort("can't set spi mode");

ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
if (ret == -1)
pabort("can't get spi mode");

/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");

ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");

/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");

printf("spi mode: 0x%x\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

d=transfer(fd,channel,delay);
printf("%x",d);

close(fd);

return d;
}

这是我的.h文件代码
#ifndef SPI_H
#define SPI_H

extern "C" {
void pabort(const char *s);
unsigned short int transfer(int fd,unsigned short int channel,unsigned short int delay);
void parse_opts();
int ADcollect(unsigned short int channel,unsigned int ADspeed,unsigned short int delay);
}
#endif

就是不知道为什么我都是按照网上大家说的做得一编译就出这个问题

真的没治了 向大家请教怎么解决这个
...全文
909 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueice12 2018-04-24
  • 打赏
  • 举报
回复
extern "C" 这个指令是在C++代码中用的,C语言不支持。如果你的代码后缀是.c,那就不能用这个关键字,直接去掉就行了。如果你这段放在.h中可能给.c也可能给.cpp包含,那就这样写: #ifdef __cplusplus extern "C" { #endif ........ #ifdef __cplusplus } #endif

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧