I’m currently working on a project that includes multiple source files and header files. I have a weird problem that occurs when I declare a function in a header file.
The problem is as following: In a source file named kernel.cpp I implement a few functions in the following manner:
#include "kernel.h"
void printf(uint8_t* str)
{
static uint16_t* VideoMemory = (uint16_t*) 0xb8000;
static uint8_t x = 0, y = 0;
for (int32_t i = 0; str[i] != ''; ++i)
{
switch(str[i])
{
case 'n': // New line
y++;
x = 0;
VideoMemory[80*y + x] = (VideoMemory[80*y + x]&0xFF00) | (int32_t)">";
break;
default:
VideoMemory[80*y + x] = (VideoMemory[80*y + x]&0xFF00) | str[i];
x++;
break;
}
if (x >= 80)
{
y++; // Next line
x = 0;
}
if (y >= 25)
{
for (y = 0; y < 25; y++)
for (x = 0; x < 80; x++)
VideoMemory[80*y + x] = (VideoMemory[80*y + x]&0xFF00) | ' '; // Clear screen
x = 0;
y = 0;
}
}
}
extern "C" void fill_keyboard_buffer(uint8_t letter)
{
static int32_t index = 0;
command_buffer[index] = letter;
printf(&letter);
keyboard_buffer[index] = letter;
if (index < 127)
{
index++;
}
if (letter == 'n')
{
index = 0;
}
}
In addition to the kernel.cpp file I have another file named kernel.h in which I declare these functions. The header file is as following:
#ifndef __KERNEL_H
#define __KERNEL_H
#include "types.h"
#define KEYBOARD_BUFFER_SIZE 128
extern "C" void fill_keyboard_buffer(uint8_t letter);
void printf(uint8_t* ptr);
#endif
for clarification, the types.h header file is declared in the following fashion:
#ifndef __TYPES_H
#define __TYPES_H
typedef char int8_t;
typedef unsigned char uint8_t; // signed byte, unsigned byte
typedef short int16_t;
typedef unsigned short uint16_t; // signed 2 bytes, unsigned 2 bytes
typedef int int32_t;
typedef unsigned int uint32_t; // signed 4 bytes, unsigned 4 bytes
typedef long long int int64_t;
typedef unsigned long long int uint64_t; // signed 8 bytes, unsigned 8 bytes
#endif
For some reason, when I compile the kernel file I get the weird error ” error: expected constructor, destructor, or type conversion before ‘extern’ ” that happens in the line ” extern “C” void fill_keyboard_buffer(uint8_t letter); ” in the header file.
any idea?
I tried a few solution like defining the function in the header file in the following fashion:
#ifdef __cplusplus
extern "C" {
#endif
void fill_keyboard_buffer(uint8_t letter);
#ifdef __cplusplus
}
#endif
Also, this problem happens if I completely remove the function declaration as well, but this time with the printf function.