Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined reference to a function (gcc linking problems)

I am having problems compiling and linking these files:

MCP23017.h:

#ifndef MCP23017_H_
#define MCP23017_H_


#include <stdint.h>
// Declare functions

static int mcp23017_set_dir(int iic, uint8_t addr,  uint8_t a, uint8_t b);

static int mcp23017_write(int iic, uint8_t addr, uint8_t a, uint8_t b);
static int mcp23017_write_a(int iic, uint8_t addr, uint8_t a);
static int mcp23017_write_b(int iic, uint8_t addr, uint8_t b);
#endif

MCP23017.c:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include "MCP23017.h"
static int mcp23017_set_dir(int iic, uint8_t addr,  uint8_t a, uint8_t b)
{
    return 0;
}



static int mcp23017_write(int iic, uint8_t addr, uint8_t a, uint8_t b)
{
    return 0;
}

static int mcp23017_write_a(int iic, uint8_t addr, uint8_t a)
{
    return 0;
}

static int mcp23017_write_b(int iic, uint8_t addr, uint8_t b)
{
    return 0;
}

and ICflash.c:

#include "MCP23017.h"

int main(int argc, char* argv[])
{
    mcp23017_set_dir(iic, I2C_EX1, IO_DIR_BUS_AD_OUT, IO_DIR_BUS_AD_OUT);
    mcp23017_set_dir(iic, I2C_EX2, IO_DIR_BUS_AD_OUT, IO_DIR_BUS_AD_OUT);
    mcp23017_write(iic, I2C_EX1, 0x00, 0x00);
    mcp23017_write(iic, I2C_EX2, 0x00, 0x00);
}

I get this errors:

gcc MCP23017.c ICflash.c -o ICflash

In file included from ICflash.c:25:
MCP23017.h:28:12: warning: ‘mcp23017_set_dir’ used but never defined
   28 | static int mcp23017_set_dir(int iic, uint8_t addr,  uint8_t a, uint8_t b);
      |            ^~~~~~~~~~~~~~~~
MCP23017.h:30:12: warning: ‘mcp23017_write’ used but never defined
   30 | static int mcp23017_write(int iic, uint8_t addr, uint8_t a, uint8_t b);
      |            ^~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccfa811I.o: in function `main':
ICflash.c:(.text+0xec): undefined reference to `mcp23017_set_dir'
/usr/bin/ld: ICflash.c:(.text+0x100): undefined reference to `mcp23017_set_dir'
/usr/bin/ld: ICflash.c:(.text+0x114): undefined reference to `mcp23017_write'
/usr/bin/ld: ICflash.c:(.text+0x128): undefined reference to `mcp23017_write'
collect2: error: ld returned 1 exit status

I have removed a lot of the code. This is just an example.
If i put it all in the "main.c" i compiles fine.

i have also tried to compile both .c files first and then link it. .. same result :/

where did i miss something ?

regards!

>Solution :

Static functions in C are functions that are restricted to the same file in which they are defined

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading