Page 1 of 2

Need help with multiple C files

Posted: Sun Dec 20, 2015 11:09 am
by moondeck
Hi, i am new here, i am moondeck and i am doing my OS (i guess you could call it that...)
Since i have written some amount of code, it started to get too messy to write in one file, so i decided to make a new directory for kernel IO code, but now the compiler wont compile it, and i have no idea why. Can someone please help me?

kernel.c

Code: Select all

#include "kernelio/kernelio.h"

void kernel_main(){
	clearscreen();
	kout("moonKERNEL v0.01");
	return;
}
kernelio/kernelio.c

Code: Select all

typedef unsigned int	 u32int;
typedef					int	 s32int;
typedef unsigned short u16int;
typedef					short s16int;
typedef unsigned char	u8int;
typedef					char	s8int;
unsigned int lin = 0;
unsigned int col = 0;

#define BITSET(var,pos) ((var) & (1<<(pos)))

char kout_colorvar = 0x04;

void kout(char *kouttext){
	char *videoMem = (char*)0xb8000;
	while (*kouttext != 0) {
		videoMem[col] = *kouttext++;
		videoMem[col+1] = kout_colorvar;
		col = col + 2;
	}
	col = 80*2;
}

void kout_color(char kout_color){
	kout_colorvar = kout_color;
}

void clearscreen(){
	volatile char *videoMem = (volatile char*)0xb8000;
	for (volatile int counterforclean = 0; counterforclean < (80*25*2); counterforclean++) {
		*videoMem++ = 0x00;
	}
	col = 0;
	lin = 0;
}

void outb(u16int port, u8int value){
		asm("outb %1, %0" : : "dN" (port), "a" (value));
}

u16int inb(u16int port){
    u8int ret;
    asm volatile ( "inb %1, %0" : "=a"(ret) : "Nd"(port) );
    return ret;
}

void kin(char *kintext){

}
kernelio.h

Code: Select all

void kout(char *kouttext);
void kout_color(char kout_color);
void clearscreen();
void kin(char *kintext);
void outb(u16int port, u8int value);
u16int inb(u16int port);
makefile

Code: Select all

kernel:
	nasm -f elf32 kernelld.asm -o k.o
	gcc -m32 -c kernelio/kernelio.c
	gcc -m32 -c kernel.c
	gcc -m32 -o kernel_main.o kernel.o kernelio.o
	ld -m elf_i386 -T kernel.ld -o kernel k.o kernel_main.o
clean:
	rm -f kernel
Error

Code: Select all

nasm -f elf32 kernelld.asm -o k.o
gcc -m32 -c kernelio/kernelio.c
gcc -m32 -c kernel.c
gcc -m32 -o kernel_main.o kernel.o kernelio.o
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../../lib/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../../lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../../lib/libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../../lib/libc.a when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libc.a when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../libc.a when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/libc.a when searching for -lc
/usr/bin/ld: cannot find -lc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../../lib/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../libgcc.a when searching for -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../../lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib/../lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib64/gcc/x86_64-solus-linux/5.2.0/../../../libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: skipping incompatible /usr/lib/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'kernel' failed
make: *** [kernel] Error 1
Sorry if the code is bad :/

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 11:27 am
by Techel
So the compiler says "no, I don't feel like to compile"?

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 11:31 am
by moondeck
Roflo wrote:So the compiler says "no, I don't feel like to compile"?
Sorry, forgot about that :D Edited now.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 11:32 am
by iansjack
No error messages? That's unusual.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 12:27 pm
by Roman
Looks like you're not using a cross-compiler. Your linker tries to link your 32-bit code with a 64-bit libgcc.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 12:29 pm
by moondeck
Roman wrote:Looks like you're not using a cross-compiler.
No, why should i? I dont really get it.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 12:31 pm
by Roman
moondeck wrote:
Roman wrote:Looks like you're not using a cross-compiler.
No, why should i? I dont really get it.
http://wiki.osdev.org/GCC_Cross-Compiler
Your GCC is built for AMD64 and GNU/Linux, it's not recommended to assume, that it won't include target-specific stuff. Read more in the link above.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 12:32 pm
by moondeck
Roman wrote:Looks like you're not using a cross-compiler. Your linker tries to link your 32-bit code with a 64-bit libgcc.
And it worked before i moved some functions to a separate file, but i will try to set up a cross compiler

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 1:46 pm
by moondeck
Roman wrote:
moondeck wrote:
Roman wrote:Looks like you're not using a cross-compiler.
No, why should i? I dont really get it.
http://wiki.osdev.org/GCC_Cross-Compiler
Your GCC is built for AMD64 and GNU/Linux, it's not recommended to assume, that it won't include target-specific stuff. Read more in the link above.
Thanks for helping, i will try to make a toolchain. Can anyone explain me if i really need a GDT? It says that most c compilers assume flat memory, so why would i need it anyway? Is there something i dont understand?

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 1:53 pm
by iansjack
You need a GDT. Segment registers in protected and long modes are indexes into this table. So even with a flat memory model you need the descriptors.

The Intel Programmer's Manual gives a very full description of all this; it is essential reading.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 2:32 pm
by moondeck
iansjack wrote:You need a GDT. Segment registers in protected and long modes are indexes into this table. So even with a flat memory model you need the descriptors.

The Intel Programmer's Manual gives a very full description of all this; it is essential reading.
The thing with 3000something pages? Well, i will take a look. Thanks for helping.

EDIT: I have compiled the cross-compiler, but i get this error after "making":

Code: Select all

nasm -f elf32 kernelld.asm -o k.o
i686-elf-gcc -m32 -c kernelio/kernelio.c
i686-elf-gcc -m32 -c kernel.c
i686-elf-gcc -m32 -o kernel_main.o kernel.o kernelio.o
/home/moondeck/ccompiler/lib/gcc/i686-elf/5.3.0/../../../../i686-elf/bin/ld: cannot find crt0.o: No such file or directory
/home/moondeck/ccompiler/lib/gcc/i686-elf/5.3.0/../../../../i686-elf/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'kernel' failed
make: *** [kernel] Error 1
Do i need some libraries or what?

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 3:07 pm
by Roman
Remove "-m32", add "-ffreestanding".
i686-elf-gcc -m32 -o kernel_main.o kernel.o kernelio.o
Are you sure this is correct? This line tells the compiler to link kernel.o and kernelio.o, then output an executable to kernel_main.o

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 3:18 pm
by moondeck
Roman wrote:Remove "-m32", add "-ffreestanding".
i686-elf-gcc -m32 -o kernel_main.o kernel.o kernelio.o
Are you sure this is correct? This line tells the compiler to link kernel.o and kernelio.o, then output an executable to kernel_main.o
Isnt it supposed to? Kernelio.h is a file included in kernel.c

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 3:40 pm
by iansjack
moondeck wrote:The thing with 3000 something pages?
The size of the manual should give you some inkling of its importance (though you don't need to read all 3000 at once). It's a complicated processor and you can't just guess at how it works. A good reference is essential, and this is the best one there is (apart, possibly, from the AMD equivalent). Just be thankful that it is free; in days gone by you had to pay hundreds of dollars for this sort of information.

Re: Need help with multiple C files

Posted: Sun Dec 20, 2015 3:46 pm
by moondeck
iansjack wrote:
moondeck wrote:The thing with 3000 something pages?
The size of the manual should give you some inkling of its importance (though you don't need to read all 3000 at once). It's a complicated processor and you can't just guess at how it works. A good reference is essential, and this is the best one there is (apart, possibly, from the AMD equivalent). Just be thankful that it is free; in days gone by you had to pay hundreds of dollars for this sort of information.
I am not saying that i am not thankful :D I am just used to microcontroller datasheets, like 600 sheets