Author Topic: hello world for linux in C++  (Read 28865 times)

0 Members and 10 Guests are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
hello world for linux in C++
« on: September 11, 2024, 10:37:59 am »
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?

 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 6418
  • Country: nz
Re: hello world for linux in C++
« Reply #1 on: September 11, 2024, 10:49:42 am »
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: [Select]
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$

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: [Select]
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:~$
« Last Edit: September 11, 2024, 10:53:14 am by brucehoult »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #2 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. 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.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 6418
  • Country: nz
Re: hello world for linux in C++
« Reply #3 on: September 11, 2024, 11:28:24 am »
You can have spaces in file and directory names, no problem, if you want to.

Code: [Select]
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!

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.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12464
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #4 on: September 11, 2024, 02:40:16 pm »
well i got burnt the first time but the program had a file called windows.h included.
This mans that your program likely calls WinAPI functions, and it is not going to link on Linux.

and there is a .out file, surely that is not the finished program? I thought linux programs had no extension.
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.
Alex
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #5 on: September 11, 2024, 03:50:33 pm »
right so sounds like I have a basic working setup.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #6 on: September 11, 2024, 05:09:49 pm »
So if I create a generic console app instead of a Linux one I can write a program that will also compile under Linux providing I don't include windows.h
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12464
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #7 on: September 11, 2024, 05:15:05 pm »
Yes, if you only use only functions from the standard  C library, it will compile and work everywhere.

In general, for cross platform development it is easier to focus on the Linux side and use MinGW to compile on Windows. You get a rich ecosystem of cross platform libraries.

But if you need Windows specific stuff, then it must be separated into the platform files, which are unique for each platform.
Alex
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #8 on: September 11, 2024, 05:20:50 pm »
Functions from the standard C library? but I want to do this in C++, I realize that pretty much all of the C stuff either works in C++ or was rewritten for C++ but if windows.h gives access to windows only stuff and I avoid that, what is the next gotcha I have to look out for?

Basically I'd like to develop with visual studio on windows and be able to run the results on Linux. My next step for example is to start looking at the FLTK libraries so my assumption is that as I can compile the libraries for either platform what runs in one will run in the other.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12464
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #9 on: September 11, 2024, 05:30:29 pm »
but I want to do this in C++
Well, C++ has sttd:: namespace. Same principle.

if windows.h gives access to windows only stuff and I avoid that, what is the next gotcha I have to look out for?
It is hard to tell the extent of things you want to do. If you are fine doing basic console applications, then there is not much that can go wrong. But if you want to interact with outside word, like using USB or networking, you will have to interact with the OS to some extent.

Basically I'd like to develop with visual studio on windows and be able to run the results on Linux.
Assuming VS only supports MSVC as a compiler, you may also need to deal with minor differences between MSVC and GCC. There is nothing major, but there are differences.

My next step for example is to start looking at the FLTK libraries so my assumption is that as I can compile the libraries for either platform what runs in one will run in the other.
No idea, I have not looked at FLTK. It should be possible to figure out, but I would not expect it to be trivial.

The most straightforward way to do cross platform graphical applications is Qt, but their licensing and pricing sucks.
Alex
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #10 on: September 11, 2024, 06:42:44 pm »
OK so it sounds like I will be best off using a linux specific console application and connect to my debian machine.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5734
  • Country: Earth
Re: hello world for linux in C++
« Reply #11 on: September 11, 2024, 07:35:12 pm »
You can start with this command in Linux:
Code: [Select]

printf '#include <stdio.h>\nint main() { printf("Hello World!\\n"); return 0; }\n' > hello.c && gcc hello.c -o hello && ./hello


:)

Regarding to C vs C++, I recommend you to start from C and when you will be able to use structs to represent different entities, you will be ready to switch to C++.

If you want to develop UI app on VS under Windows and then run it on Linux. I think the most easy way is to develop it in C# using WinForms, use .NET FW 4.

You can run it on linux using mono. Also on Linux you can open your project and compile in MonoDevelop, but you will not be able to use WinForms designer to edit window layout on Linux. For Linux there is GTK# form designer.

Here is hello world example in C# using WinForms:
Code: [Select]
using System;
using System.Windows.Forms;

class Program {
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
       
        using (var form = new Form())
        {
            form.Text = "Hello World Form";

            var button = new Button();
            button.Text = "Click me";
            button.AutoSize = true;
            button.Location = new System.Drawing.Point(
                (form.ClientSize.Width - button.Width) / 2,
                (form.ClientSize.Height - button.Height) / 2
            );

            button.Click += (sender, e) => MessageBox.Show("Hello World");

            form.Controls.Add(button);

            Application.Run(form);
        }   
    }
}

This example opens window with a button "Click me". When you click it, it will show you message "Hello world".

You can compile it with this command on linux:
Code: [Select]

mcs -r:System.Windows.Forms -r:System.Drawing helloworld.cs


then you can run it using this command on linux:
Code: [Select]
mono helloworld.exe

On Windows you can compile it with
Code: [Select]

csc /r:System.Windows.Forms.dll /r:System.Drawing.dll helloworld.cs


If you want better cross-platform support you can use other libraries.
« Last Edit: September 11, 2024, 08:14:32 pm by radiolistener »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #12 on: September 11, 2024, 08:10:07 pm »
Um structs, you mean those things that are all over the micro controller code that I program, yes quite, I use structs quite a lot. My main reason for looking to move on to C++ is that it will have broader library support and fltk runs with C++ not C. I have not done anything with it but I gather that it is as close as one gets to a C++ native graphical library.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5734
  • Country: Earth
Re: hello world for linux in C++
« Reply #13 on: September 11, 2024, 08:20:25 pm »
if you select language for MCU, it's better to stay with C style even if you use C++ compiler.
This is because using C++ libraries leads to more large and slower bloated code which require more memory. This is not good for MCU.

Regarding UI for MCUs, it depends very much on which MCU you use. Not many MCUs are capable of running full-featured UI libraries.
« Last Edit: September 11, 2024, 08:28:01 pm by radiolistener »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18890
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #14 on: September 11, 2024, 08:27:51 pm »
uh, I regularly program MCU's in C. I now want to start looking at programming in C++ for linux on single board computers, totally different ball game.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5734
  • Country: Earth
Re: hello world for linux in C++
« Reply #15 on: September 11, 2024, 08:34:30 pm »
If your SBC supports OpenGL or OpenGL ES, I can recommend to use Dear ImGui.

It runs very well on Raspberry Pi Zero 2W in KMS DRM mode using SDL2 backend.

If you know C, I think it will be easy to use it even if you don't familiar with C++, despite the fact that it is written in C++.

It is very simple, lightweight, very fast and allows to implement cool professional UI with minimal effort.

There are numerous ready-to-use widgets available for ImGui that cover nearly any use case. :)
« Last Edit: September 11, 2024, 08:51:45 pm by radiolistener »
 

Offline Postal2

  • Frequent Contributor
  • **
  • !
  • Posts: 826
  • Country: 00
Re: hello world for linux in C++
« Reply #16 on: September 12, 2024, 01:34:50 am »
You are confusing C++ and MFC. In fact, it turns out that for portability between different Linuxes you will have to drag and drop the entire graphics subsystem. In the same way, you can use a Windows emulator and MFC, and the development speed is higher, and the weight of the software is comparable.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 6418
  • Country: nz
Re: hello world for linux in C++
« Reply #17 on: September 12, 2024, 02:23:29 am »
The most straightforward way to do cross platform graphical applications is Qt, but their licensing and pricing sucks.

Arguably, an HTML-based UI is easier, certainly if your UI needs are not demanding. Either with an HTTP server library added to your app, or with an HTTP server app running your app as a CGI.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17783
  • Country: fr
Re: hello world for linux in C++
« Reply #18 on: September 12, 2024, 02:50:04 am »
Um structs, you mean those things that are all over the micro controller code that I program, yes quite, I use structs quite a lot. My main reason for looking to move on to C++ is that it will have broader library support and fltk runs with C++ not C. I have not done anything with it but I gather that it is as close as one gets to a C++ native graphical library.

Using FLTK is not unreasonable if you want to develop simple, cross-platform GUI stuff that doesn't requires gigabytes of install and a 6-month learning curve.
It's certainly simpler and much more lightweight than Qt.

And this may help get you started with the tooling: https://www.fltk.org/articles.php?L1820
 

Offline Postal2

  • Frequent Contributor
  • **
  • !
  • Posts: 826
  • Country: 00
Re: hello world for linux in C++
« Reply #19 on: September 12, 2024, 03:16:07 am »
Arguably, an HTML-based UI is easier, ...
As an option, you can carry with your program several assemblies of the customized browser for different Linuxes.
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5734
  • Country: Earth
Re: hello world for linux in C++
« Reply #20 on: September 12, 2024, 05:58:03 am »
The most straightforward way to do cross platform graphical applications is Qt, but their licensing and pricing sucks.

Arguably, an HTML-based UI is easier, certainly if your UI needs are not demanding. Either with an HTTP server library added to your app, or with an HTTP server app running your app as a CGI.

Yeah, when I see these gigabytes of libraries and all the issues with versioning just to implement a simple window with buttons, it looks like a nightmare to support software written with Qt. Debugging is another issue. I know of one software that raises a segmentation fault exception somewhere inside Qt, and when you see the stack trace and all the code that needs to be analyzed, it's a nightmare to figure out what's going wrong.

Currently, I'm looking for a lightweight desktop UI for my tools (different kinds of calculators, debuggers, DSP processing, etc.). I first considered Qt, but after testing it, I decided to avoid this nightmare >:(

Also tried Rust with some UI libraries, but when the folder with a simple example swelled to several gigabytes of libraries (which also require to update rust compiler) and after some fiddling with the compatibility of package versions, just to write a simple window, I decided that this is not what I need...

Currently, I’ve written part of my tools in HTML and JavaScript, and they can be run from a browser or using Python with the python3-webview library. For example:
Code: [Select]
import webview

window = webview.create_window('Calculator', url='~/mycalc.html')
webview.start()

for other tools I'm using C# with OpenGL or WinForms backend and C++ with ImGui.

And still looking for a good cross-platform solution to replace old WinForms with something more modern, lightweight and beautiful.
« Last Edit: September 12, 2024, 06:04:56 am by radiolistener »
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3910
  • Country: it
Re: hello world for linux in C++
« Reply #21 on: September 12, 2024, 07:26:38 am »
And still looking for a good cross-platform solution to replace old WinForms with something more modern, lightweight and beautiful.

yeah :( and the answer is NOT tkinter.

I routinely have the same problem myself.. i DO NOT want to make another VB6 application for that simple task, because VB6's basic is archaic, i do not want to use visual studio, VBNET or C# (and visual c++ these days is confusing to me), in the end i go back to Qt. Qt is really comfy once you get past the first couple of applications, the problem is having to drag 50-60 MB of libraries every time. Sucks but i have not found a better solution

(well, with a couple of more libraries one can use LVGL so the application could eventually be ported to a bare metal embedded solution... but then you have to do GUIs in C)
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 5734
  • Country: Earth
Re: hello world for linux in C++
« Reply #22 on: September 12, 2024, 10:04:55 am »
i do not want to use visual studio, VBNET or C# (and visual c++ these days is confusing to me), in the end i go back to Qt.

I think C# looks the best on Linux and MacOS from this point of view. The executable is very small and can run with no recompilation on any platform with better speed than Qt based app and eats much less memory. I even tried to run the multiplayer network game server written in C# on Raspberry Pi Zero 2W and surprisingly it works pretty smooth with no lags...  :)

I also have OpenGL multiplayer game client written in C# which uses my own cross-platform OpenGL engine, it is small and works on Raspberry Pi 4 better than other C++ and Web versions from another developers on Windows with i5 CPU... :)

I like that C# code is so tiny, fast and easy to read and debug. The only issue with C# is a lack of supported dev tools on Linux. MonoDevelop works good, but its unfortunately abandoned and don't have WinForms editor. And I don't want to use vscode due to telemetry.

So, I think the recipe probably is to use C# with MonoDevelop, but replace WinForms with some more modern cross platform UI library. And I am in the process of searching.

(well, with a couple of more libraries one can use LVGL so the application could eventually be ported to a bare metal embedded solution... but then you have to do GUIs in C)

I don't see the reason to use bare metal apps. Using C++ app and ImGui with SDL2 backend you can run nice UI app with no X11, just lightweight linux kernel with drivers for DRM/KMS acceleration.

This is how ImGui interface looks:
« Last Edit: September 12, 2024, 10:25:17 am by radiolistener »
 

Offline Postal2

  • Frequent Contributor
  • **
  • !
  • Posts: 826
  • Country: 00
Re: hello world for linux in C++
« Reply #23 on: September 12, 2024, 03:36:18 pm »
.....This is how ImGui interface looks:
MFC does this in 3 minutes:
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5325
  • Country: nl
Re: hello world for linux in C++
« Reply #24 on: September 12, 2024, 06:30:40 pm »
Regarding to C vs C++, I recommend you to start from C and when you will be able to use structs to represent different entities, you will be ready to switch to C++.

Bad advise. C and C++ are two very different languages, even though they have a very similar syntax. The thoughts behind oject oriented programming is also quite different.

A "Hello Wold" in C++ is easy for the command line. Just a few lines of code that spit out characters. It's when you want to get into GUI applications that things start getting complicated.

A long time ago (25 years or so) I used the Borland C++ builder and clicked together some GUI applications, and that worked quite well, I really liked how Borland C++ Builder worked. But I did not have many applications to write, and it got dusted over. A bit over 10 years ago I switched to Linux, and I have not (knowingly) used a microsoft product after that. I did dabble a bit with QT Creator for a bit, but they are constantly attempting to annoy their open source part and I started to dislike them for that, so I ditched QT and the QT Creator. (Even though I quite liked the IDE. I have used it for a while for programming AVR microcontrollers.

Every now and then I have a short look at wxWidgets. It's also a cross platform windowing framework with bindings to a lot of languages. You can write a wxWidgets GUI program in 15 lines of Python (but I don't like python). Recently I accidentally bumped into the Codelite IDE. It seems to have a very nice mix of simplicity but yet being powerful enough to be taken seriously. I installed it, created a wxWidgets program (In C++), compiled it and it just all worked. I have also used CodeBlocks for a while to write microcontroller programs. It also has "built in" support for writing wxWidget GUI applications, but I had a lot of trouble to get that to work. One of the reasons I am (a bit) interested in wxWidgets is that it's also used by KiCad. KiCad is struggling a bit with it. QT is more powerful, but wxWidgets is more then capable for anything I would want to do with it. Debugging (breakpoints, single stepping and such) also "just worked" in the Codelite IDE.

And now I (accidentally) installed an IDE win which wxWidgets "just works", it's getting time to read more about wxWidgets, and how those "sizers" work. It's not as easy and intuitive as the Borland C++ Builder was 25 years ago and I find myself in need of some kind of book or tutorial to get started in a decent way with wxWidgets. But getting my STM32 programs away from that eclipse monstrosity has a higher priority for me, but that's a different chapter of a different book.
« Last Edit: September 12, 2024, 06:34:59 pm by Doctorandus_P »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf