Page 1 of 1
using c and c++
Posted: Sun Jul 03, 2016 3:49 am
by cklie97
Hello there. While writing my kernel I'm now at a point where I want to use both c, because most of my code is written in it and c++, because of classes.
At the moment I use "make" to help me compiling, but I don'nt get it why the Makefile I wrote for c and c++ do not work.
Makefile
Code: Select all
SRCS = $(shell find ./code -name '*.[cppS]')
OBJS = $(addsuffix .o,$(basename $(SRCS)))
CC = gcc
CPP = g++
LD = ld
ASFLAGS = -m32
CFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc
CPPFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc
LDFLAGS = -melf_i386 -Tcode/kernel.ld
kernel: $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
%.o: %.cpp
$(CPP) $(CPPFLAGS) -c -o $@ $^
%.o: %.S
$(CC) $(ASFLAGS) -c -o $@ $^
clean:
rm $(OBJS)
.PHONY: clean
The problem:
- My *.cpp files are not compiled (missing *.o files)
Re: using c and c++
Posted: Sun Jul 03, 2016 3:57 am
by iansjack
Output from the make command?
Re: using c and c++
Posted: Sun Jul 03, 2016 4:05 am
by cklie97
Make output:
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/string.o code/string.c
gcc -m32 -c -o code/int_stub.o code/int_stub.S
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/keyboard.o code/keyboard.c
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/interrupt_descriptor_table.o code/interrupt_descriptor_table.c
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/memory_manager.o code/memory_manager.c
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/global_descriptor_table.o code/global_descriptor_table.c
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/assembler.o code/assembler.c
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/main.o code/main.c
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/console.o code/console.c
gcc -m32 -c -o code/start.o code/start.S
gcc -m32 -Wall -g -fno-stack-protector -nostdinc -c -o code/shell.o code/shell.c
ld -melf_i386 -Tcode/kernel.ld -o kernel code/string.o code/int_stub.o code/keyboard.o code/interrupt_descriptor_table.o code/memory_manager.o code/global_descriptor_table.o code/assembler.o code/main.o code/console.o code/start.o code/shell.o
Re: using c and c++
Posted: Sun Jul 03, 2016 4:07 am
by gerryg400
I think this is wrong. This means *.c or *.p or *.S. It doesn't match with cpp at all.
Re: using c and c++
Posted: Sun Jul 03, 2016 4:25 am
by cklie97
Thanks that helped. Now the *.cpp files are getting compiled.
new Makefile
SRCS = $(shell find ./code -name '*.[cS]' -or -name '*.cpp')
[...]
But now I have a problem with the linker.
Output
Code: Select all
ld -melf_i386 -Tcode/kernel.ld -o kernel code/string.o code/int_stub.o code/keyboard.o code/interrupt_descriptor_table.o code/memory_manager.o code/global_descriptor_table.o code/assembler.o code/main.o code/console.o code/start.o code/shell.o code/t32.o
code/main.o: In Funktion `main':
code/main.c:13: Nicht definierter Verweis auf `test'
main.c
Code: Select all
[...]
#include "t32.h"
10 int main(void)
11 {
12 init();
13 test();
14 while(1);
15 return 0;
16 }
[...]
t32.h
t32.cpp
Re: using c and c++
Posted: Sun Jul 03, 2016 5:12 am
by heat
cklie97 wrote:Thanks that helped. Now the *.cpp files are getting compiled.
new Makefile
SRCS = $(shell find ./code -name '*.[cS]' -or -name '*.cpp')
[...]
But now I have a problem with the linker.
Output
Code: Select all
ld -melf_i386 -Tcode/kernel.ld -o kernel code/string.o code/int_stub.o code/keyboard.o code/interrupt_descriptor_table.o code/memory_manager.o code/global_descriptor_table.o code/assembler.o code/main.o code/console.o code/start.o code/shell.o code/t32.o
code/main.o: In Funktion `main':
code/main.c:13: Nicht definierter Verweis auf `test'
main.c
Code: Select all
[...]
#include "t32.h"
10 int main(void)
11 {
12 init();
13 test();
14 while(1);
15 return 0;
16 }
[...]
t32.h
t32.cpp
I'm not sure if you know what C++ name decoration/mangling is... i suggest you learn more about the linking process.
Re: using c and c++
Posted: Sun Jul 03, 2016 6:42 am
by Roman
Don't forget C++ names are mangled to contain type information. If you want a C++ function to be callable from C, you need to declare it with extern "C".
Re: using c and c++
Posted: Sun Jul 03, 2016 7:16 am
by Octocontrabass
You should be using a
cross-compiler, too.