Build C-writed apps in windows

215 views
Skip to first unread message

gz

unread,
May 28, 2011, 5:43:57 PM5/28/11
to BareMetal OS
How I can build my apps written in c with windows? Can I use Visual
Studio?

Thanks

Ian Seyler

unread,
May 29, 2011, 8:04:06 PM5/29/11
to BareMetal OS
I'm not entirely sure. It would depend on Visual Studios ability to
create a flat binary executable.

-Ian

Bartłomiej Pietrzyk

unread,
May 30, 2011, 3:18:03 AM5/30/11
to bareme...@googlegroups.com
Could you explain the term "flat" :) ??

2011/5/30 Ian Seyler <ise...@gmail.com>



--
Pozdrawiam
Bartłomiej Pietrzyk

42Bastian

unread,
May 30, 2011, 3:35:42 AM5/30/11
to bareme...@googlegroups.com
Am 30.05.2011 09:18, schrieb Bart�omiej Pietrzyk:
> Could you explain the term "flat" :) ??

Though I am not sure what Ian means, you should be able to build the
application
a) without any reference to the windows runtime
b) no DLL dependencies
c) linked to a fixed address (there is no loader to relocate your code and
data)

--
42Bastian
+
| http://www.sciopta.com
| Fastest direct message passing kernel.
| IEC61508 certified.
+

RC

unread,
May 30, 2011, 10:12:42 PM5/30/11
to bareme...@googlegroups.com, ise...@gmail.com
C compiler problems... Somebody could help me, please? :-/

Compiler: gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.5.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.5.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo
mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --disable-werror --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.5.2 (GCC)

Command: gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -fomit-frame-pointer -mno-red-zone -o helloc.o helloc.c
helloc.c:1:0: sorry, unimplemented: 64-bit mode not compiled in

Thank you very much in advance... Best regards,

"If you have an apple and I have an apple and we exchange apples then you and I will still each have one apple.
But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas."
(George Bernard Shaw)

"The only thing necessary to the triumph of evil, is that good men do nothing."
(Edmund Burke)



2011/5/30 42Bastian <list-bast...@sciopta.com>

Esteban Ordano

unread,
May 30, 2011, 10:23:13 PM5/30/11
to bareme...@googlegroups.com, ise...@gmail.com
Looks like you are in a 32 bit environment, so you need to have cross-compiling. You have to have it configured when you build the compiler. Looks like cygwin doesn't defaults that option to true. You can adventure into trying to compile it yourself (read this guide: http://gcc.gnu.org/install/configure.html)

I suggest you to have a 64 bit Operating System to avoid having to cross-compile. If you want to use Windows, remember also that you should compile with -no-cygwin.

Regards,
Esteban

RC

unread,
May 30, 2011, 11:56:55 PM5/30/11
to bareme...@googlegroups.com, ise...@gmail.com
Thanx ESTEBAN,

You right, now I have a MINGW64 installed and I could compile the HELLOC.C file, but... :-/

When I try LINK with LIBBAREMETAL.O something is wrong wet... :-/

Command: ld -T app.ld -o helloc.app helloc.o libBareMetal.o
ld: cannot perform PE operations on non PE output file 'helloc.app'.

BTW... When I compile the KERNEL, using NASM (2.10 RC4)...

Command: nasm -f bin -o kernel64.sys kernel64.asm
syscalls/debug.asm:147: warning: signed dword immediate exceeds bounds
syscalls/debug.asm:148: warning: signed dword immediate exceeds bounds

That's OK or I need to fix someting parameters? :-/

Thank you once again for your attention and help... Best regards,

"If you have an apple and I have an apple and we exchange apples then you and I will still each have one apple.
But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas."
(George Bernard Shaw)

"The only thing necessary to the triumph of evil, is that good men do nothing."
(Edmund Burke)



Ian Seyler

unread,
May 31, 2011, 1:50:20 PM5/31/11
to BareMetal OS
It looks like the linker is trying to create a Windows (PE) binary.
I'll see what I can find on MINGW64.

The NASM warnings can be ignored. YASM does not show these messages.
I'll take a look at this as well as I would like to switch the
official compiler back to NASM as it is updated more frequently.

-Ian


On May 30, 11:56 pm, RC <r...@piclistbr.com> wrote:
> Thanx ESTEBAN,
>
> You right, now I have a MINGW64 installed and I could compile the HELLOC.C
> file, but... :-/
>
> When I try LINK with LIBBAREMETAL.O something is wrong wet... :-/
>
> *Command: ld -T app.ld -o helloc.app helloc.o libBareMetal.o
> *ld: cannot perform PE operations on non PE output file 'helloc.app'.
>
> BTW... When I compile the KERNEL, using NASM (2.10 RC4)...
>
> *Command: nasm -f bin -o kernel64.sys kernel64.asm*

RC

unread,
May 31, 2011, 2:07:30 PM5/31/11
to bareme...@googlegroups.com
That's true, IAN... :)

But when I compile the HELLO application, for example, using the NASM compiler (hello.napp) and also using the YASM compiler (hello.yapp, using version yasm-1.1.0-win64)... The result is not the same (see attached, please)... :-/

FYI... Unfortunately I don't have a disassembler tools to check it out... :-/

Can I use both and I will not have problem with these diferences?

Thank you for attention and help... :)

Best regards,

"If you have an apple and I have an apple and we exchange apples then you and I will still each have one apple.
But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas."
(George Bernard Shaw)

"The only thing necessary to the triumph of evil, is that good men do nothing."
(Edmund Burke)



hello.napp
hello.yapp

Ian Seyler

unread,
May 31, 2011, 4:00:53 PM5/31/11
to BareMetal OS
A very interesting find! Both versions work correctly but the YASM
compiled version is only 28 bytes (vs NASM's 31 bytes). NASM used a
true 64-bit assignment while it looks like YASM used a sign extended
assignment.

The YASM version is only ideal if the value is 32-bit of less. The
NASM version will always be correct.

C:\baremetal\utils>ndisasm -b 64 hello.napp
00000000 48BE100020000000 mov rsi,0x200010
-0000
0000000A E80100F0FF call dword 0xfffffffffff00010
0000000F C3 ret
00000010 ...

C:\baremetal\utils>ndisasm -b 64 hello.yapp
00000000 48C7C60D002000 mov rsi,0x20000d
00000007 E80400F0FF call dword 0xfffffffffff00010
0000000C C3 ret
0000000D ...

-Ian
>  hello.napp
> < 1KViewDownload
>
>  hello.yapp
> < 1KViewDownload

RC

unread,
May 31, 2011, 4:22:03 PM5/31/11
to bareme...@googlegroups.com
WOW... Great, now I know that I have a disassembler tools (NDISASM)... :)

Thank you very much for your help, IAN... ;)

Question... I'm using Windows environment, because is more easy to me at this moment... Well... When you said "both versions work correctly", I think that you loaded both APPs in your BARE-METAL and you tested them... Am I right?

There's some way to load some APPs (Bare Metal Application) compiled on Windows environment to the QEMU environment to test them?

Thanx once again for your help and attention... :)

Best regards,

"If you have an apple and I have an apple and we exchange apples then you and I will still each have one apple.
But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas."
(George Bernard Shaw)

"The only thing necessary to the triumph of evil, is that good men do nothing."
(Edmund Burke)



Ian Seyler

unread,
Jun 1, 2011, 1:22:29 PM6/1/11
to BareMetal OS
Yes, I tested both versions and they worked as expected.

The easiest way under Windows is with a program called WinImage:
http://www.winimage.com/

WinImage can open the disk image file and you can drag and drop files
to it.

-Ian

RC

unread,
Jun 1, 2011, 1:26:57 PM6/1/11
to bareme...@googlegroups.com
WOW... Now I am really HAPPY... Works... :) lol

Thank you very much IAN... ;)

Best regards,

"If you have an apple and I have an apple and we exchange apples then you and I will still each have one apple.
But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas."
(George Bernard Shaw)

"The only thing necessary to the triumph of evil, is that good men do nothing."
(Edmund Burke)



Jay

unread,
Jul 3, 2011, 3:59:47 PM7/3/11
to BareMetal OS
I am stuck at this point too


C:\BareMetalSource\programs>x86_64-w64-mingw32-ld -T app.ld -o
helloc.app helloc.o libBareMetal.o
x86_64-w64-mingw32-ld: cannot perform PE operations on non PE output
file 'helloc.app'.

The above is the error I get.

I can compile the c code fine but when I try to link the above error.

Jay

On May 30, 11:56 pm, RC <r...@piclistbr.com> wrote:
> Thanx ESTEBAN,
>
> You right, now I have a MINGW64 installed and I could compile the HELLOC.C
> file, but... :-/
>
> When I try LINK with LIBBAREMETAL.O something is wrong wet... :-/
>
> *Command: ld -T app.ld -o helloc.app helloc.o libBareMetal.o
> *ld: cannot perform PE operations on non PE output file 'helloc.app'.
>
> BTW... When I compile the KERNEL, using NASM (2.10 RC4)...
>
> *Command: nasm -f bin -o kernel64.sys kernel64.asm*

Ian Seyler

unread,
Jul 3, 2011, 8:32:46 PM7/3/11
to BareMetal OS
I will need to look into it. I compile the C apps under Linux.

Most likely you will need to compile to a native Windows executable
(PE) and then strip it with objcopy.. At least I think that would
work.

-Ian

Jay

unread,
Jul 4, 2011, 9:17:30 AM7/4/11
to BareMetal OS
x86_64-w64-mingw32-gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -
fomit-frame-pointer -finline-functions -o helloc.o helloc.c
x86_64-w64-mingw32-gcc -c -m64 -nostdlib -nostartfiles -nodefaultlibs -
fomit-frame-pointer -finline-functions -o libBareMetal.o
libBareMetal.c
x86_64-w64-mingw32-objcopy --remove-section .eh_frame --remove-
section .rel.eh_frame --remove-section .rela.eh_frame helloc.o
x86_64-w64-mingw32-objcopy --remove-section .eh_frame --remove-
section .rel.eh_frame --remove-section .rela.eh_frame libBareMetal.o
x86_64-w64-mingw32-ld -T app.ld -o helloc.app helloc.o libBareMetal.o


This is what I am doing now, I still get

x86_64-w64-mingw32-ld: cannot perform PE operations on non PE output
file 'helloc.app'.

on the linker
> > > > Esteban- Hide quoted text -
>
> - Show quoted text -

Ian Seyler

unread,
Jul 7, 2011, 2:14:56 PM7/7/11
to BareMetal OS
Please provide *exact* details on this. What binaries did you download
for the compiler and from where?

-Ian

Dan Humphrey

unread,
Apr 8, 2012, 7:44:50 PM4/8/12
to bareme...@googlegroups.com
Hi Guys - new to the group, but wondering if there was ever any resolution to the linker problem and PE output. Apologies if the answer turned up in another post.
Thanks,
Dan

Ian Seyler

unread,
Apr 11, 2012, 10:07:26 AM4/11/12
to bareme...@googlegroups.com
Here are the instructions I have so far. Once I have something more complete I will put it in the wiki.

Building C/C++ apps in Windows for BareMetal OS

Introduction

The usual development environment for BareMetal OS is linux which usually has the compiler tools pre-installed. This document contains the instructions necessary for compiling C/C++ applications for BareMetal OS under Windows.

Installing the compiler

You can download the latest version of GCC for Windows from here (Make sure to get tdm64-gcc-X.X.X):

http://tdm-gcc.tdragon.net/download

Run the installer once it has finished downloading.

In the setup click on 'Create'.

Select 'MinGW-w64/TDM64 Experimental (32-bit and 64-bit)' and click next.

The default installation directory is fine for most installs. Click next.

For a download mirror the SourceForge Default is fine. Click next.

Select 'All Packages' for the install type. Click next.

Once the installation is complete click next.

Deselect the ReadMe file option and click finish.

Compiling Your Application

We can start with some very basic code that uses libBareMetal.

#include "libBareMetal.h"

int start(void)
{
    b_print_string("Hello world, from C!\n");
    return 0;
}

Note that the program starts in start() instead of main().

And then the compile:

gcc -m64 -nostdlib -nostartfiles -nodefaultlibs -mno-red-zone -o hello.o hello.c libBareMetal.c -DBAREMETAL -Ttext=0x200000
objcopy -O binary hello.o hello.app

Dan Humphrey

unread,
Apr 11, 2012, 4:07:24 PM4/11/12
to bareme...@googlegroups.com
Success - tx.

Ian Seyler

unread,
Apr 11, 2012, 9:51:41 PM4/11/12
to bareme...@googlegroups.com
Great to hear. I added the doc to the wiki.



On Wednesday, April 11, 2012 4:07:24 PM UTC-4, Dan Humphrey wrote:
Success - tx.
Reply all
Reply to author
Forward
0 new messages