Help with gcc and ld in Dev-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
KingTomato
Posts: 9
Joined: Sun Aug 05, 2007 12:58 am
Location: Arkansas

Help with gcc and ld in Dev-C++

Post by KingTomato »

Hey I know for those of you who read my other thread about the Pascal OS. I decided to try a C kernel first. I didn't make this I got it from a tutorial on osdever.net

Here is my code

Code: Select all

#define WHITE_TXT 0x07

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);
void update_cursor(int row, int col);

k_main()
{
        k_clear_screen();
        k_printf("Hello World", 0);
        };
        
        void k_clear_screen()
        {
             char *vidmem=(char *) 0xb800;
             unsigned int i=0;
             while(i < (80*25*2))
             {
                     vidmem[i]=' ';
                     i++;
                     vidmem[i]=WHITE_TXT;
                     i++;
                     };
                     };
                     
                     unsigned int k_printf(char *message, unsigned int line)
                     {
                              char *vidmem = (char *) 0xb8000;
                              unsigned int i=0;
                              
                              i=(line*80*2);
                              
                              while(*message!=0)
                              {
                                                if(*message=='\n')
                                                {
                                                                  line++;
                                                                  i=(line*80*2);
                                                                  *message++;
                                                                  } else {
                                                                         vidmem[i]=*message;
                                                                         *message++;
                                                                         i++;
                                                                         vidmem[i]=WHITE_TXT;
                                                                         i++;
                                                                         };
                                                                         };
                                                                         return (1);
                                                                         };
and my asm file

Code: Select all

[BITS 32]
[global start]
[extern _k_main];

start:
call _k_main

cli ; 	
hlt ;
Now the tutorial I read said use nasm -f aout kernel_start.asm -o ks.o
Then do gcc -c kernel.c - o kernel.o
Then the final step which was the linking I was suposed to use ld -T link.ld -o kernel.bin ks.o kernel.o

Now I downloaded NASM, and Dev-C++ comes with a thing called gcc and ld, I'm supposing those are what I need, but I dont know where to type in those comands, I tried cmd prompt and it didint work, and I dont know where else to try. I'm on windows, so please help me if you can. Thanks
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Your 'PATH' is probably off.

Also, you might want to try Cygwin instead of Mingw.
C8H10N4O2 | #446691 | Trust the nodes.
KingTomato
Posts: 9
Joined: Sun Aug 05, 2007 12:58 am
Location: Arkansas

Post by KingTomato »

What path? the gcc and ld are in C:\Dev-C++\bin and thats the same places that are listed within the Dev-C++ Environment, Any other ideas?

I've also been downloading Cygwin, but I'm on dial so it takes awhile. Thanks for the reply.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

What does

Code: Select all

C:\Dev-C++\bin\gcc --version
do? Does it print anything? or give a file not found?

JamesM
KingTomato
Posts: 9
Joined: Sun Aug 05, 2007 12:58 am
Location: Arkansas

Post by KingTomato »

Ok I got NASM to work, but now when I do the gcc part it says it can't find cc1:No such file or directory. That command you gave me Dex it wouldnt let it run, so I done cygwin -r -s -v, and it gave me a whole output of a bunch of stuff and said it found the needed files for things like ld, nasm, and gcc. So I don't know what to do about the cc1 error.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

If you're using GCC like JamesM said, then it probably can't find cc1 because your PATH variable is off. (It's an environmental variable in Windows.)

I can't recall the exact way to set it at the moment. (Google it. ;) )
C8H10N4O2 | #446691 | Trust the nodes.
KingTomato
Posts: 9
Joined: Sun Aug 05, 2007 12:58 am
Location: Arkansas

Post by KingTomato »

Ok, I go my path set and got gcc working, but now when I try to link it says D:\cygwin\bin\ld.exe cannot open the linker script linker.ld:No such fiel or directory, but the linker.ld file is in there.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

where exactly is the linker.ld file, and where are you running ld from? By that I mean, "what is the working directory of ld"? are you doing

Code: Select all

cd D:\cygwin\bin\
ld.exe linker.ld ...bleh
or (this is what you SHOULD be doing)

Code: Select all

cd my_os_home_directory
D:\cygwin\bin\ld.exe linker.ld ...bleh
Can't think of anything else atm, you're description is very vague...

JamesM
KingTomato
Posts: 9
Joined: Sun Aug 05, 2007 12:58 am
Location: Arkansas

Post by KingTomato »

Hey I have attached a screen shot showing what I have done. It still don't work, but maybe with a screen maybe yall can help. C:\ is where I have been saving all my OS stuff. I got nams and gc, to produce the output I wanted, but when I try to link, no cigar. As you can see there was one time it got past the link.ld, but said kernel.bin didnt exist. I thought that it linked and output kernel.bin so how wold it exist before i done it. Maybe I'm wrong. I'm new to all this OS ****, but please help if you can. Thanks[/img]
Attachments
cygwin.JPG
cygwin.JPG (72.7 KiB) Viewed 1670 times
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Hi,

Code: Select all

ld.exe -t link.ld -c C:\kernel.bin C:\ks.o C:\kernel.o
OK, some major things wrong with this. Firstly I don't think you understand quite how cygwin and UNIX works.

In UNIX, there are no drives. No C:\, no D:\. Everything comes from one common place called the 'root'. So, most people's home directories are in:

Code: Select all

/home/<username>/
The first slash means this is an absolute pathname, coming from the root.

I've never used cygwin, but I'm certain it exposes one directory in your C: drive as the root directory. For example:

If the cygwin root directory is C:\cygwin\, then accessing a file called C:\cygwin\mydir\myfile in bash(shell environment exposed by cygwin) would be:

Code: Select all

/mydir/myfile
Another thing to note is NEVER EVER EVER EVER use backslashes '\' in unix. EVERYTHING is forward slashes '/'. If you've ever programmed in C, you'll see the sense in this (backslash is a special character in C, you have to type 2 backslashes to escape it)

Also, your ld command should have a '-o' on it, by the kernel.bin. This is specifying that kernel,.bin is the output file, not an input file.

So, assuming that all your files are in C:\cygwin\, and the cygwin root is at c:\cygwin, your link command should be:

Code: Select all

cd /
ld -t link.ld -o kernel.bin ks.o kernel.o
Also note that linux executables DO NOT have any .exe extension after them.

JamesM
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Because of the way Cygwin works, you can reference files in much the same way as Windows: "C:/my/folder/with/file.txt"...

Slashes the other way, though, will be removed from the input stream.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

pcmattman: OK, thanks I didnt realise cygwin had that capability.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

JamesM wrote:pcmattman: OK, thanks I didnt realise cygwin had that capability.
Because it runs under Windows, it has to use the Windows I/O functions, so you can still access drives the Windows way. Cygwin just gives support for other access methods that act the same way as *nix systems.
Post Reply