using c and c++

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
cklie97
Posts: 18
Joined: Tue Apr 19, 2016 2:11 am

using c and c++

Post 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)
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: using c and c++

Post by iansjack »

Output from the make command?
cklie97
Posts: 18
Joined: Tue Apr 19, 2016 2:11 am

Re: using c and c++

Post 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
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: using c and c++

Post by gerryg400 »

Code: Select all

*.[cppS]
I think this is wrong. This means *.c or *.p or *.S. It doesn't match with cpp at all.
If a trainstation is where trains stop, what is a workstation ?
cklie97
Posts: 18
Joined: Tue Apr 19, 2016 2:11 am

Re: using c and c++

Post 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

Code: Select all

void test();
t32.cpp

Code: Select all

#include "t32.h"

void test()
{
}
heat
Member
Member
Posts: 103
Joined: Sat Mar 28, 2015 11:23 am
Libera.chat IRC: heat

Re: using c and c++

Post 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

Code: Select all

void test();
t32.cpp

Code: Select all

#include "t32.h"

void test()
{
}
I'm not sure if you know what C++ name decoration/mangling is... i suggest you learn more about the linking process.
If some of you people keep insisting on having backwards compatibitity with the stone age, we'll have stone tools forever.
My Hobby OS: https://github.com/heatd/Onyx
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: using c and c++

Post 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".
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: using c and c++

Post by Octocontrabass »

cklie97 wrote:

Code: Select all

CC = gcc
CPP = g++
You should be using a cross-compiler, too.
Post Reply