Game Development Community

C++ Hello world without <iostream> ?

by User97684 · in General Discussion · 11/26/2015 (7:08 am) · 11 replies

i know this is not related to garage games. but i think "You" can help me.

when we start learning a new programming language, we make something like this

// C++ hello world hello world program
#include <iostream>

int main()
{
   std::cout << "Hello, World!";
   return 0;
}

But i need to make my code run on multiple platforms.
some of that doesn't support "Input Output Stream".

#1
11/26/2015 (7:52 am)
iostream is required for C++ compliance, else it isn't C++. You could do C style instead:
#include <cstdio>

int main(int argc, char *argv[])
{
  printf("Hello World\n");
  return 0;
}
#2
11/26/2015 (8:32 am)
You could also write your own custom I/O library in assembly for each platform using a common interface and use that....

Which platforms are we talking about?
#3
11/26/2015 (10:12 pm)
nothing. Just i need to know

"How I/O Libraries Work?" ,
"What is the changes made in Operating Systems to Support IO?",
"How can we write our own library?"
and
Is writing a I/O library is useful or useless?
#4
11/27/2015 (8:31 am)
Sounds like you have a lot of homework to do.

I/O libraries work by providing a consolidated interface to an underlying set of input/output functionality provided either directly by the hardware or by the operating system.

Changes made in operating systems to support I/O depend entirely on the hardware on which they're designed to run....

Examine your target platform, learn how I/O is handled on it, write platform-specific code where necessary that handles I/O through a common interface.

It depends - if you come up with a solid interface then it's useful. If you write it yourself you choose to make it good or bad - The question here is "what set of functionality do I need for my games?" Answer that, learn to handle it on all of your target platforms, design your library to utilize the correct underlying OS functionality based on the compile target. At that point, your game code uses the same interface across all platforms - thus simplifying your life from that point forward. Whenever you want support for a new platform, examine the new target and provide code that works there.
#5
12/01/2015 (4:20 am)
What homework?
This is my question, # not a homework.
#6
12/01/2015 (5:48 am)
If you knew what you were asking....

Look pal. Writing an I/O library is not a trivial undertaking. I've explained the fundamental principles behind it but I'm not going to spend two months writing and debugging a system for you. Go grab some books and figure it out. The reason I'm not going to do it for you is simply this:
; ----------------------------------------------------------------------------------------
; Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
; To assemble and run:
;
;     nasm -felf64 hello.asm && ld hello.o && ./a.out
; ----------------------------------------------------------------------------------------

        global  _start

        section .text
_start:
        ; write(1, message, 13)
        mov     rax, 1                  ; system call 1 is write
        mov     rdi, 1                  ; file handle 1 is stdout
        mov     rsi, message            ; address of string to output
        mov     rdx, 13                 ; number of bytes
        syscall                         ; invoke operating system to do the write

        ; exit(0)
        mov     eax, 60                 ; system call 60 is exit
        xor     rdi, rdi                ; exit code 0
        syscall                         ; invoke operating system to exit
message:
        db      "Hello, World", 10      ; note the newline at the end
# ----------------------------------------------------------------------------------------
# Writes "Hello, World" to the console using only system calls. Runs on 64-bit Linux only.
# To assemble and run:
#
#     gcc -c hello.s && ld hello.o && ./a.out
#
# or
#
#     gcc -nostdlib hello.s && ./a.out
# ----------------------------------------------------------------------------------------

        .global _start

        .text
_start:
        # write(1, message, 13)
        mov     , %rax                # system call 1 is write
        mov     , %rdi                # file handle 1 is stdout
        mov     $message, %rsi          # address of string to output
        mov     , %rdx               # number of bytes
        syscall                         # invoke operating system to do the write

        # exit(0)
        mov     , %rax               # system call 60 is exit
        xor     %rdi, %rdi              # we want return code 0
        syscall                         # invoke operating system to exit
message:
        .ascii  "Hello, world\n"
Both run on some base 64 bit Linux kernel, but the syntax is different between assemblers. Note that these are just "Hello World."

Have fun.
#7
12/02/2015 (8:32 pm)
24 Line code just outputs only 1 line, that we can do in c++ with iostream with just 6 line code.

I think the iostream library contains 100s or 1000s of lines of code!.
is above program is possible only on a low level language?
if yes, Can we do it in 'C'?.
or did we have to learn more than hello world in 'Assembly'?

Looks like both directly calling the linux kernel. unfortunately i am running
on windows. is iostream calls windows kernel to display "Hello, World!"?

//as my current programming knowledge, i know this is too much for me
i have to google your words to know their meaning and usage
but i learned a lot from this forum, thank you! \
#8
12/03/2015 (5:48 am)
If you're running on Windows then you can use std::iostreams! In your original post you specifically stated "platforms that don't support iostreams"....
#9
12/04/2015 (3:03 am)
Sorry for indirect information. I just needed to know how I/O Libraries works.
My windows can run IOSTREAM Codes.

here what i learned:
I/O libraries are shortcuts to contact the kernel!.
our code calls I/O, that send the information Got from our code
to the kernel of operating system.
am i correct?

sorry again, for indirect info.
#10
12/04/2015 (5:58 am)
Basically, yeah, but some libraries talk directly to the hardware - particularly in embedded systems where there is no hardware abstraction layer.

When supporting multiple platforms, you have to understand I/O on each target platform then write I/O wrapper calls that your program can call instead of the platform-specific ones. The wrapper then calls the correct low-level call to handle the task on the current platform.
#11
12/04/2015 (8:04 am)
Thank you.
This will be very useful for me.

(unfortunately here is no like button)