OSDev.org
https://forum.osdev.org/

Problem converting meaty skeleton to work with CPP
https://forum.osdev.org/viewtopic.php?f=1&t=33678
Page 1 of 1

Author:  heavyweight87 [ Fri May 03, 2019 2:42 am ]
Post subject:  Problem converting meaty skeleton to work with CPP

Hi,

I would like to start taking the meaty skeleton project and change the kernel.c to kernel.cpp
I took the meaty skeleton project and changed a few things so it would work with cpp:

* I renamed kernel.c to kernel.cpp
* I added extern "C" on function prototypes where needed
* I added export CPP=${HOST}-g++ to config.sh
* I added ffreestanding -nostdlib -fno-exceptions -fno-rtti to CCFLAGS in the makefile
* I added this to the makefil:
.cpp.o:
$(CPP) -MD $(CPPFLAGS) $< -o $@

The problem is I get undefined refernces to any external functions e.g. printf, serial_init, terminal_initialize

Im sure the problem is quite basic but I have less experience with C++ and makefiles

A repository with the changes I did is here:
https://github.com/heavyweight87/SakuraOS/tree/cpp

Any ideas?

Author:  mallard [ Fri May 03, 2019 2:49 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

Make sure you've deleted any old .o files that may have been generated from C compilation.

Are the external functions that you're having problems with declared as 'extern "C"' or not? Are they defined in C or C++ code? Could you post the complete compiler/linker output?

Author:  heavyweight87 [ Fri May 03, 2019 3:04 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

Yeh I made sure I did a clean before.
I wrapped the function prototypes in the .h file with the extern "C".

output from the compiler:


mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/lib
cp libk.a /home/gcb/Projects/myos/SakuraOS/sysroot/usr/lib
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
i686-elf-g++ --sysroot=/home/gcb/Projects/myos/SakuraOS/sysroot -isystem=/usr/include -MD -O0 -g -D__is_kernel -Iinclude -ffreestanding -nostdlib -Wall -Wextra -fno-exceptions -fno-rtti kernel/kernel.cpp -o kernel/kernel.o
kernel/kernel.cpp: In function 'int kernel_main(uint32_t, multiboot_info_t*)':
kernel/kernel.cpp:89:37: warning: array subscript has type 'char' [-Wchar-subscripts]
printf("%c", kbdus[x]);
^
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008048074
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /tmp/ccZo95GY.o: in function `load_modules(multiboot_info*)':
/home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:56: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /tmp/ccZo95GY.o: in function `kernel_main(unsigned long, multiboot_info*)':
/home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:67: undefined reference to `terminal_initialize'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:68: undefined reference to `serial_init'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:69: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:72: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:75: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:89: undefined reference to `printf'

Author:  bellezzasolo [ Fri May 03, 2019 3:37 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

heavyweight87 wrote:
Yeh I made sure I did a clean before.
I wrapped the function prototypes in the .h file with the extern "C".

output from the compiler:


mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/lib
cp libk.a /home/gcb/Projects/myos/SakuraOS/sysroot/usr/lib
mkdir -p /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include
cp -R --preserve=timestamps include/. /home/gcb/Projects/myos/SakuraOS/sysroot/usr/include/.
i686-elf-g++ --sysroot=/home/gcb/Projects/myos/SakuraOS/sysroot -isystem=/usr/include -MD -O0 -g -D__is_kernel -Iinclude -ffreestanding -nostdlib -Wall -Wextra -fno-exceptions -fno-rtti kernel/kernel.cpp -o kernel/kernel.o
kernel/kernel.cpp: In function 'int kernel_main(uint32_t, multiboot_info_t*)':
kernel/kernel.cpp:89:37: warning: array subscript has type 'char' [-Wchar-subscripts]
printf("%c", kbdus[x]);
^
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008048074
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /tmp/ccZo95GY.o: in function `load_modules(multiboot_info*)':
/home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:56: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /tmp/ccZo95GY.o: in function `kernel_main(unsigned long, multiboot_info*)':
/home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:67: undefined reference to `terminal_initialize'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:68: undefined reference to `serial_init'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:69: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:72: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:75: undefined reference to `printf'
/home/gcb/opt/cross/lib/gcc/i686-elf/7.4.0/../../../../i686-elf/bin/ld: /home/gcb/Projects/myos/SakuraOS/kernel/kernel/kernel.cpp:89: undefined reference to `printf'

It looks like you're referencing extern C functions that aren't there. Have you made the functions themselves extern "C", rather than just the prototypes?

Author:  heavyweight87 [ Fri May 03, 2019 3:50 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

I thought the extern "C" only went around the prototype?
I tried adding it to the function definition and it didn't change anything...

More than that __cplusplus is never defined in printf.c for example .I guess that makes sense no?

The strange thing is that all the object files are present, so im not sure why it can't find the functions

Author:  mallard [ Fri May 03, 2019 4:22 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

heavyweight87 wrote:
More than that __cplusplus is never defined in printf.c for example .I guess that makes sense no?


Yes, "__cplusplus" will only be defined in C++ code. Never in C code. Differentiating between C and C++ compilers is pretty much what it's for. I assume therefore you have the 'extern "C"' in the .h file wrapped in "#ifdef __cplusplus" so that it doesn't upset the C compiler?

Is "kernel.cpp" actually including the same .h file that contains the 'extern "C"' definitions? I presume you haven't done something like writing the definitions directly into the source file...

Author:  heavyweight87 [ Fri May 03, 2019 4:28 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

so for example, kernel.cpp references serial.h
(you can see here: https://github.com/heavyweight87/Sakura ... l/serial.h)

serial.h looks like this:

Code:
#ifndef __KERNEL_SERIAL_H_
#define __KERNEL_SERIAL_H_

#ifdef __cplusplus
extern "C" {
#endif

void serial_init(void);
void serial_printchar(char a);
int serial_printf(const char* __restrict format, ...);

#ifdef __cplusplus
}
#endif

#endif //__KERNEL_SERIAL_H_

Author:  heavyweight87 [ Fri May 03, 2019 5:33 am ]
Post subject:  Re: Problem converting meaty skeleton to work with CPP

I found the problem

I added this to the makefile:
.cpp.o:
$(CPP) -MD $(CPPFLAGS) $< -o $@

but I missed the -c flag which tells gcc to compile without linking

Thanks for your help, everything works fine :)

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/