Products > Embedded Computing

hello world for linux in C++

(1/30) > >>

Simon:
I have decided that it is time to start getting into writing programs for linux in C++.

I am aware that the system libraries are all different between windows and linux.

Where do I start? I have installed debian on a laptop and attempted to use a hello world program I found on the internet in visual studio code, well the system header file did not exist apparently.

I have visual studio on my windows PC but this is not linux. I had a working program but it can't compile for linux and it was a very poorly written one at that but it showed that my visual studio was working.

Next I tried a c++ program for linux project in VS and I managed to connect to my debian laptop. I got a bash error saying that there were too many arguments for cd

Then I tried a cmake project but really at this point I have no idea and as this is cross platform I don't know if it is for windows or linux.

All I want is a working setup that will produce a hello world program that will run on linux.

What do I do?

brucehoult:
It doesn't matter what OS you are on, the C or C++ standard library will work just the same for programs that read and write files, including stdin and stdout.


--- Code: ---bruce@i9:~/programs$ cat hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}
bruce@i9:~/programs$ g++ hello.cpp -o hello
bruce@i9:~/programs$ ./hello
Hello World!
bruce@i9:~/programs$

--- End code ---

That happens to be on Ubuntu on Windows 11, but it will work exactly the same on native Linux (obviously), or MacOS or Linux on Arm or RISC-V etc etc.


--- Code: ---bruce@i9:~/programs$ scp hello.cpp lp:
hello.cpp                                                                                 100%   96     8.6KB/s   00:00   
bruce@i9:~/programs$ ssh lp

   ____              _ ____  ____  _  __  ____  _                     _
  |  _ \ _   _ _   _(_) ___||  _ \| |/ / / ___|(_)_ __   ___  ___  __| |
  | |_) | | | | | | | \___ \| | | | ' /  \___ \| | '_ \ / _ \/ _ \/ _` |
  |  _ <| |_| | |_| | |___) | |_| | . \   ___) | | |_) |  __/  __/ (_| |
  |_| \_\\__,_|\__, |_|____/|____/|_|\_\ |____/|_| .__/ \___|\___|\__,_|
               |___/                             |_|                   
                                        -- Presented by ISCAS and Sipeed

  Debian GNU/Linux 12 (bookworm) (kernel 5.10.113-lpi4a)

Linux lpi4a 5.10.113-lpi4a #2024.01.01.02.29+2bd2eac4a SMP PREEMPT Mon Jan 1 02:30:07 UTC 2 riscv64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri Aug 23 16:47:25 2024 from 192.168.1.169
debian@lpi4a:~$ g++ hello.cpp -o hello
debian@lpi4a:~$ ./hello
Hello World!
debian@lpi4a:~$ file hello
hello: ELF 64-bit LSB pie executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, BuildID[sha1]=a16873f88fc47464ae68d2b28df89f7b10b39437, for GNU/Linux 4.15.0, not stripped
debian@lpi4a:~$

--- End code ---

Simon:
well i got burnt the first time but the program had a file called windows.h included. So if there is no windows.h included can i assume that I won't be able to use windows only stuff?

I have now correctly compiled a linux console program making sure I don't have spaces in any file or folder name and there is a .out file, surely that is not the finished program? I thought linux programs had no extension.

brucehoult:
You can have spaces in file and directory names, no problem, if you want to.


--- Code: ---debian@lpi4a:~$ ls -l *.cpp
-rw-r--r-- 1 debian debian 96 Sep 11 10:52 'hello world.cpp'
debian@lpi4a:~$ g++ 'hello world.cpp' -o 'hello world'
debian@lpi4a:~$ ./'hello world'
Hello World!

--- End code ---

If you don't tell the compiler what output name you want then it uses a.out for historical compatibility reasons dating back to the early 1970s.

Unix file names don't have extensions, but they can have dots in their names if they want to, and this can be used to indicate the file contents.

ataradov:

--- Quote from: Simon on September 11, 2024, 11:15:21 am ---well i got burnt the first time but the program had a file called windows.h included.

--- End quote ---
This mans that your program likely calls WinAPI functions, and it is not going to link on Linux.


--- Quote from: Simon on September 11, 2024, 11:15:21 am ---and there is a .out file, surely that is not the finished program? I thought linux programs had no extension.

--- End quote ---
There are no meaningful extensions. "a.out" is just a file name that includes period. This is the default output file name for a very long historic reasons. The "OUT" was an actual binary format before ELF existed. Then everyone moved to ELF, but the name stuck. The actual contents is ELF, of course, and it can just run.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod