Need help with multiple C files

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.
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Need help with multiple C files

Post 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 :/
Last edited by moondeck on Sun Dec 20, 2015 11:32 am, edited 1 time in total.
Techel
Member
Member
Posts: 215
Joined: Fri Jan 30, 2015 4:57 pm
Location: Germany
Contact:

Re: Need help with multiple C files

Post by Techel »

So the compiler says "no, I don't feel like to compile"?
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

Post by moondeck »

Roflo wrote:So the compiler says "no, I don't feel like to compile"?
Sorry, forgot about that :D Edited now.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Need help with multiple C files

Post by iansjack »

No error messages? That's unusual.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Need help with multiple C files

Post 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.
Last edited by Roman on Sun Dec 20, 2015 12:29 pm, edited 1 time in total.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

Post by moondeck »

Roman wrote:Looks like you're not using a cross-compiler.
No, why should i? I dont really get it.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Need help with multiple C files

Post 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.
Last edited by Roman on Sun Dec 20, 2015 12:32 pm, edited 1 time in total.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

Post 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
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

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

Re: Need help with multiple C files

Post 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.
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

Post 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?
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Need help with multiple C files

Post 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
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

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

Re: Need help with multiple C files

Post 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.
User avatar
moondeck
Member
Member
Posts: 56
Joined: Sat Dec 19, 2015 12:18 pm
Libera.chat IRC: moondeck
Location: The Zone, Chernobyl

Re: Need help with multiple C files

Post 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
Post Reply