Undefined reference to `LCD12864_init()'

I don’t know why I get an error when I call a function.
Just put ‘LCD12864 init();’ comment it out, and it will compile normally.
Please help to answer, thank you.

error message

c:/users/xiang/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: .pio\build\uno_r4_wifi\src\main.cpp.o: in function `loop':
main.cpp:(.text.loop+0x0): undefined reference to `LCD12864_init()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\uno_r4_wifi\firmware.elf] Error 1

lcd12864.c

#include "lcd12864.h"
#include <Arduino.h>


//写cmd函数
void LCD12864_write_cmd(int cmd){
    digitalWrite(LCD12864_RS,LOW);
    digitalWrite(LCD12864_RW,LOW);
    digitalWrite(LCD12864_EN,LOW);
    //缺少数据的输出定义cmd
    delay(1000);
    digitalWrite(LCD12864_EN,HIGH);
    delay(1000);    
    digitalWrite(LCD12864_EN,LOW);   
}

//写data函数
void LCD12864_write_data(int data){
    digitalWrite(LCD12864_RS,HIGH);
    digitalWrite(LCD12864_RW,LOW);
    digitalWrite(LCD12864_EN,LOW);
    //缺少数据的输出定义data
    delay(1000);
    digitalWrite(LCD12864_EN,HIGH);
    delay(1000);    
    digitalWrite(LCD12864_EN,LOW);   
}


//显示屏初始化函数
void LCD12864_init(void){
    digitalWrite(LCD12864_PSB,HIGH);
    //控制指令
    LCD12864_write_cmd(0X30);//功能设定 001DL X0/REXX
    LCD12864_write_cmd(0X0C);//显示状态开关 00001DCB
    LCD12864_write_cmd(0X06);//进入设定点 000001 1/D S
    LCD12864_write_cmd(0X31);//清屏
    

}

void LCD12864_clear(void){
    LCD12864_write_cmd(0X31);//清屏
}

void LCD12864_show_string(int x,int y,char *str){
    if(y<=0){
        y=0;
    }
    if(y>3){
        y=3;
    }
    x&=0x0f;

    
    //直接显示的地址
    switch (y)
    {
    case 0:x|=0x80;break;
    case 1:x|=0x90;break;
    case 2:x|=0x88;break;
    case 3:x|=0x98;break;

    }
    LCD12864_write_cmd(x);
    while (*str!='\0')
    {
        LCD12864_write_data(*str);
        str++;
    }
    
}

lcd12864.h

#ifndef _lcd12864_H
#define _lcd12864_H

#include<Arduino.h>
const int LCD12864_RS = 8;
const int LCD12864_RW = 9;
const int LCD12864_EN = 10;
const int LCD12864_PSB = 12;  //高电平为8位
//缺少0~7端口的定义 对应数据口的定义data


// Declare Functions
void LCD12864_init();
void LCD12864_clear(void);
void LCD12864_show_string(int x,int y,char *str);


#endif

main.cpp

#include <Arduino.h>
#include "lcd12864.h"


void setup() {
	pinMode(LCD12864_RS,OUTPUT);
	pinMode(LCD12864_RW,OUTPUT);
	pinMode(LCD12864_EN,OUTPUT);
	pinMode(LCD12864_PSB,OUTPUT);
}

void loop() {
	LCD12864_init();
	//LCD12864_show_string(0,0,"Hello World");
}

You’re mixing .c with .cpp code and yet you don’t expose the functions as extern "C" linkage when included from C++ in the header.

You need Combining C++ and C - how does #ifdef __cplusplus work? - Stack Overflow.

#ifndef _lcd12864_H
#define _lcd12864_H

#include<Arduino.h>

#ifdef __cplusplus
extern "C" {
#endif

const int LCD12864_RS = 8;
const int LCD12864_RW = 9;
const int LCD12864_EN = 10;
const int LCD12864_PSB = 12;  //高电平为8位
//缺少0~7端口的定义 对应数据口的定义data


// Declare Functions
void LCD12864_init();
void LCD12864_clear(void);
void LCD12864_show_string(int x,int y,char *str);

#ifdef __cplusplus
}
#endif

#endif

Note: Your code is very unusual.

Arduino in its core is a C++ based framework, and using it in a .c file only gives you acces to the basic functions but none of the C++ classes, like Serial, Wire, SPI et cetera. So what you should really do is simply rename lcd12864.clcd12864.cpp. That’s it. Then you also don’t ned the #ifdef __cplusplus in the header. Plus you can actually use all of the Arduino functions and classes.

1 Like

Thank you so much for providing two methods, as expected, the compilation was successful after changing to.cpp.