Including and in the same project

前端 未结 2 756
逝去的感伤
逝去的感伤 2020-12-20 22:33

What I want to achieve: I want to set custom baud rate values for some tty*-like UART-mapped terminals.

How: The only way I fo

相关标签:
2条回答
  • 2020-12-20 22:41

    I had a similar issue - wanted custom baud rate support with definitions like termios2, TCGETS2 and BOTHER, while still making use of the traditional termios calls. I instinctively wrapped the termios2 stuff in its own compilation unit and had no problems. So my structure looks like this:

    serial_driver.c/.h

    #include <termios.h>
    #include <fcntl.h>
    #include <dirent.h>
    
    int open_port(int fd, int baudrate, eParitySetting parity, int numStopBits)
    {
      if(baudrate_is_non_standard(baudrate)
        setNonStandardBaudRateTermios(fd, baudrate, parity, numStopBits);
      else
        //all the normal tcgetattr/cfsetospeed/tcsetattr
    }
    
    int do_other_things(void)
    {
      //all the normal tcsendbreak/tcflush/etc things
    }
    

    serial_driver_abr.c/.h

    #include <asm/termios.h> /* asm gives us the all important BOTHER and TCGETS2 */
    #include <fcntl.h>
    #include <dirent.h>
    #include <stropts.h> /* Oddly, for ioctl, because ioctl.h causes include dramas */
    
    setNonStandardBaudRateTermios(int fd, int baudrate, eParitySetting parity, int numStopBits)
    {
      //All the termios2/ioctl/TCGETS2/BOTHER things
    }
    

    Works well for me.

    0 讨论(0)
  • 2020-12-20 23:04

    How can I solve this without using some obscure practice (like wrapping one of headers in a namespace, like mentioned here)?

    If you find namespaces obscure, I don't know how you'd call this:

    #define termios asmtermios
    #include <asm/termios.h>
    #undef  termios
    #include <termios.h>
    

    Anyway, this too gets you rid of the error: redefinition of 'struct termios'.

    0 讨论(0)
提交回复
热议问题