-1
  • System: RPi 3 B
  • OS: Ubuntu 16.04 Mate desktop.

Uninstalled pre-installed v2.32 wiringPi package. Downloaded latest version from the wiringPi site. Ran build. The build worked; gpio readall command worked.

Now trying to compile a simple program to talk to a MCP23017 expander and getting the messages below:

/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `crypt'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `rint'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pthread_join'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pthread_create'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pow'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `shm_open'
/usr/lib/gcc/arm-linux-gnueabihf/5/../../../../lib/libwiringPi.so: undefined reference to `pthread_cancel'
collect2: error: ld returned 1 exit status
Compilation failed.

Compile command:
gcc -Wall -o "pi3b_mcp23017_01" "pi3b_mcp23017_01.c" -lwiringPi (in directory: /home/jwgr0se/projects/pi3b_mcp23017_01)

Source:
/*-------------------------------------------------------------------------------
 * pi3b_mcp23017_01.c
 * 
 */

/*-------------------------------------------------------------------------------
 * Includes.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>

/*-------------------------------------------------------------------------------
 * Global constants & variables.
 */

#define ERROR           -1
#define MCP23017_ADDR   0x20
#define BANK_SEP        0x80
#define SEQOP_DIS       0x40
#define HAEN_ENB        0x08

// MCP23017 register addresses.
typedef enum reg {
    IODIRA = 0x00,   IODIRB = 0x01,     IPOLA = 0x02,   IPOLB = 0x03,   GPINTENA = 0x04,
    GPINTENB = 0x05, DEFVALA = 0x06,    DEFVALB = 0x07, INTCONA = 0x08,  INTCONB = 0x09,  
    IOCONA = 0x0a,   IOCONB = 0x0b,     GPPUA = 0x0c,   GPPUB = 0x0d,   INTFA = 0x0e,
    INTFB = 0x0f,    INTCAPA = 0x10,    INTCAPB = 0x11, GPIOA = 0x12,   GPIOB = 0x13,
    OLATA = 0x14,    OLATB = 0x15
} MCP23017_reg;

/*-------------------------------------------------------------------------------
 * Helper functions.
 */

// FUTURE. Thread function to toggle a GPIO pin high/low.
//
void *thread_function(void *arg) {
    // Cast the paramater into what is needed.
    int *_arg = (int *)arg;

    // Do whatever is neccessary using *arg as the argument.
    for(;;) {

    }

    // The thread terminates when this function returns.
    return(NULL);
} /* End thread_function */

// Print an 8-bit register value in binary.
// 
const char* printb8(int x) {
    static char b[9];
    b[0] = '\0';
    int z;
    for (z = 128; z > 0; z >>= 1) {
        strcat(b, ((x & z) == z) ? "1" : "0");
    }
    return b;
} /* End printb */

// Press Any Key To Continue.
//
void paktc(){
    printf("Press any key to continue...\n");
    char ch = getchar();
} /* End paktc */

/*-------------------------------------------------------------------------------
 * Main function.
 */

int main(int argc, char **argv) {
    char *message = "Rpi3B - wiringPi - MCP23017 programming\n\n";
    printf("%s", message);

    // Initialize theMCP23017.
    //
    int fd = wiringPiI2CSetup(MCP23017_ADDR);
    if ( fd == ERROR) {
        printf("Initialize: There was an error setting up the MCP23017.%d\n", fd);
        exit(1);
    }
    else {
        printf("Initialize: OK\n");
    }
    paktc();

    // Set up the MCP23017 to use seperate banks of (8) I/O pins.
    //
    fd = wiringPiI2CWriteReg8(MCP23017_ADDR, IOCONA, (BANK_SEP|SEQOP_DIS|HAEN_ENB));
    if ( fd == ERROR) {
        printf("writeReg8: There was an error writing the MCP23017. %d\n", fd);
    }
    else {
        printf("writeReg8: OK\n");
        int reg_val = wiringPiI2CReadReg8(MCP23017_ADDR, IOCONA);
        printf("MCP23017: IOCONA:%5d\t%s\n", reg_val, printb8(reg_val));
    }
    paktc();

    // Set the MCP23017 bank A pins as output.
    //
    fd = wiringPiI2CWriteReg8(MCP23017_ADDR, IODIRA, 0x00);
    if ( fd == ERROR) {
        printf("There was an error writing the MCP23017.\n");
        exit(1);
    }
    else {
        printf("writeReg8: OK\n");
        int reg_val = wiringPiI2CReadReg8(MCP23017_ADDR, IODIRA);
        printf("MCP23017: IODIRA:%5d\t%s\n", reg_val, printb8(reg_val));
    }
    paktc();

    // Toggle the bank A pins high/low 10 times with a (1) second delay.
    //
    for (int count = 0; count < 10; count++) {
        // Set the MCP23017 bank A pins high.
        //
        fd = wiringPiI2CWriteReg8(MCP23017_ADDR, GPIOA, 0xff);
        delay(1000);

        // Set the MCP23017 bank A pins low.
        //
        fd = wiringPiI2CWriteReg8(MCP23017_ADDR, GPIOA, 0x00);
        delay(1000);
    }

    return(0);
} /* End main() */

Any help will be greatly appriciated.

enter code here
goldilocks
  • 58,849
  • 17
  • 112
  • 227
Farad'n
  • 129
  • 1
  • 11
  • Added pthread header file and modified build command to: gcc -Wall -o "%e" "%f" -lwiringPi -lm -lpthread -lcrypt -lrt Thank yoou. – Farad'n Jun 16 '18 at 17:43
  • If his answer is correct, please mark it accepted perhaps tomorrow (maybe another better answer might come in - I don't know). – NomadMaker Jun 16 '18 at 21:01
  • Joan's answer was correct. The program compiled without errors. However, wiringPiI2CWriteReg8() still returns -1'' - ERROR. I2C kernel modules are loaded. i2cdetect -y 1 returns0x20'' the address for a MCP23017. But the write function fails. Poopies... – Farad'n Jun 17 '18 at 16:49
  • This might seem silly, but do you have I2C turned on in raspi-config? – NomadMaker Jun 17 '18 at 16:55
  • No. I manually configured the I2C & SPI modules to load on boot.

    root@rpi3b2:~# lsmod | grep spi spi_bcm2835 6678 0

    root@rpi3b2:~# lsmod | grep i2c i2c_bcm2708 4834 0 i2c_dev 5859 0

    root@rpi3b2:~# i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    – Farad'n Jun 17 '18 at 19:05

2 Answers2

0

It looks like you need to at least add -pthread as an option and the math library m. I'm not sure about the other missing symbols crypt and shm_open

Try

gcc -Wall -pthread -o pi3b_mcp23017_01 pi3b_mcp23017_01.c -lwiringPi -lm

joan
  • 71,014
  • 5
  • 73
  • 106
0

Include the library options below in your compilation

-lwiringPi -lm -lpthread -lcrypt -lrt

For example

gcc -Wall -pthread -o pi3b_mcp23017_01 pi3b_mcp23017_01.c -lwiringPi -lm -lpthread -lcrypt -lrt
Greenonline
  • 2,740
  • 4
  • 23
  • 36
shivakumar
  • 23
  • 1
  • 5