找不到有效的参考

时间:2013-06-26 13:54:38

标签: c reference

我有一个用于硬件初始化的c文件和一个从中调用方法的c ++文件。当我尝试使用g ++编译它们时,我得到以下内容

g++ testPegio.cpp -o testPeg

我得到以下内容:

  

在/usr/include/c++/4.6/chrono:35:0中包含的文件中,                    来自testPegio.cpp:6:   /usr/include/c++/4.6/bits/c++0x_warning.h:32:2:错误:#error此文件需要编译器和库支持即将推出的ISO C ++标准C ++ 0x。此支持目前是实验性的,必须使用-std = c ++ 0x或-std = gnu ++ 0x编译器选项启用。

所以我试试这个:

g++ testPegio.cpp -o testPeg -std=c++0x

我得到了:

  

/tmp/cczOOOyb.o:在函数main': testPegio.cpp:(.text+0xc): undefined reference to pegio_init'中   collect2:ld返回1退出状态

以下是代码的重要摘要:

pegio.h

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>     //used for uart
#include <fcntl.h>      //used for uart
#include <termios.h>    //used for uart
#include <string.h>
#include <string>

#define BAUD_RATE B1152000
#define BYTE_SIZE CS8
#define MAX_STRING_LEN 80

#define true 1
#define false 0

typedef int bool;

extern char pegio_init(void);                                       // initialize serial port
extern char pegio_deinit(void);                                     // deinitialize serial port
extern void pegio_serial_write(char inStr[MAX_STRING_LEN]);         // serial write method
extern void pegio_serial_read(char * str);                          // serial read method
extern bool charCheck(char ch);                                     // character check

pegio.c

#include "pegio.h"

int uart0_filestream = -1;

// ** INITIALIZE THE UART **    
char pegio_init(void)
{

    uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (uart0_filestream == -1)
    {
        //ERROR - Can't open serial port
        printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
    }

    struct termios options;
    tcgetattr(uart0_filestream, &options);
    options.c_cflag = BAUD_RATE | BYTE_SIZE | CLOCAL | CREAD;   //<Set the baud rate
    options.c_iflag = IGNPAR | ICRNL;
    options.c_oflag = 0;
    options.c_lflag = 0;
    tcflush(uart0_filestream, TCIFLUSH);
    tcsetattr(uart0_filestream, TCSANOW, &options);

    return 0;
}

// ** DEINITIALIZE THE UART **
char pegio_deinit()
{
    close(uart0_filestream);
    return 0;
}

最后,testPeg.cpp

extern "C" {
#include "pegio.h"
}

#include <iostream>
#include <chrono>
#include <stdlib.h>

int main()
{
        int result = pegio_init();

        return result;
}

任何想法??

1 个答案:

答案 0 :(得分:0)

尝试以下步骤

 gcc -c pegio.c -o pegio.o
 g++ testPeg.cpp pegio.o -o testPeg -std=c++0x

g ++ testPeg.cpp pegio.o -o testPeg -std = c ++ 11