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

0 Members and 3 Guests are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #75 on: September 14, 2024, 07:16:09 pm »
... for a tool which don't needs smooth graphics UI and it's background work should not affect CPU load much it may be drawback. ...
The fact that you and your friend ataradov can't use it normally doesn't bother me at all. However, it does mean that beginners may have difficulties with ImGui.

Why do you see it your mission in life to beat everyone into submission in agreeing with you? oh, of course....
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #76 on: September 14, 2024, 07:31:44 pm »
Well a squareline studio project will not build, well cmake fails. Maybe I will just try to setup the lvgl library by myself but not sure how to go about it.
 

Offline Postal2

  • Frequent Contributor
  • **
  • !
  • Posts: 826
  • Country: 00
Re: hello world for linux in C++
« Reply #77 on: September 14, 2024, 09:07:01 pm »
Why do you see it your mission in life to beat everyone into submission in agreeing with you? oh, of course....
There are obvious things (like CAN bus), I try to explain, but I'm a really bad teacher. If the program just fills the buffer according to the current state - then it's the programmer's choice how often it does this. You can also run polling of an event that will never happen, and wonder about the resource consumption.
ImGui is really very good, it has a modern approach and you still have to work with it.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4561
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: hello world for linux in C++
« Reply #78 on: September 15, 2024, 11:47:13 am »
Python scares me, it sounds like someone came up with a swiss army knife that someone else tried to converter into a chainsaw. I don't like the whole tab thing, I see that it would be OK for little scripts but a whole language? I also tire of having to learn a new vocabulary of terms because some jerk decided that they had to really go all the way in pretending that this chain saw really is the real deal and very different from a real chainsaw so as to have me think I can't just go and get the proper tool. strings, arrays etc really don't need new names and if I have to learn all that just to talk basic python well they can shove up their wazoo for all I care unless there is a really good reason I should be using it over something else.
It should scare you. However python has libraries for almost everything. It might not be fast, but you can make something fast. Like an arduino.
Want to talk to your scope, sure. Want to write excel? Sure. Want to convert an image to ascii art, sure.
Now try those things with C/C++, you have https://github.com/uhub/awesome-c yet you may still have to do many things yourself.

But, tabs vs spaces should not be an issue anymore, your IDE does those. And there are linters for it. The worst bit is the weak typing, but there are type hints now. (it's awfully close to C# today, which is a dumpsterfire of new conflicting features of itself)

Not trying to convert you to python, just trying to paint the complete picture. C still is the best language ever invented. Everything else is basically calling C functions.

Please do show your progress! I'm curious.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #79 on: September 15, 2024, 12:06:37 pm »
Oh you don't have to convert me. I started reading a book although I guess as always I was trying to do it the hard way: actually understand what the fuck I am doing rather than just creating copy paste mashes.

I just got bored of having structures and arrays re explained to me like they are something really neat and specific to python just because they have a different name to what any other sensible language uses.

The library availability has been what had me considering it. Time is always of the essence and I hate doing something that I am sure already exists. Having said that lots of people are in a hurry when they write their libraries and make a right mess of it.

My only incentive to look at C++ was that I was getting a bit lost in C with the quite distinct separation between the language and the libraries. Try to read about the libraries and you are told that you need to know the linux interface. Start looking at the linux interface and you are told that you need a working understanding of the libraries. So I concluded that with C++ being more modern and maybe less hippy things would be clearer.

Ultimately I could not care less what the language is, I am not a fanboy of anything, I have a job to do.

So python is an interpreted language. Does this mean it will run slower? I have heared that it can be compiled now. Is this a mash up or is it a serious solution?
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4561
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: hello world for linux in C++
« Reply #80 on: September 15, 2024, 12:39:50 pm »
So python is an interpreted language. Does this mean it will run slower? I have heared that it can be compiled now. Is this a mash up or is it a serious solution?
It depends. Do you have specific performance targets to hit?
You can compile python, it will be byte-code iirc. Still needs the interpreter. There are also some gotcha's in deployment, like launching is slow, but running is not for example. (loading runtime...)
You can even compile it to C first, I've never done this. (cython)

The multi-platform architecture is a penalty on performance, but a huge gain in development. This is vastly different from C/C++ which compiles to actual machine code immediately.
Which will always be faster that any other higher language. But the question is, does the extra effort of C/C++ weigh up to the gained performance.

I learned last week you can also run native javascript, if you hate yourself again an evenening you can play with that (with node.js).
« Last Edit: September 15, 2024, 12:41:36 pm by Jeroen3 »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #81 on: September 15, 2024, 01:30:43 pm »
Well speed to launch is not critical as long as it is fast enough. This would run without the desktop. Last time I turned a RPi4 on with any old SD card and linux server it was alive in less than 30 seconds to get to the command line. From there the program would launch unless it needs more than the command line.

I'm willing to put a solid state drive in it. Well MMC or whatever they are called.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #82 on: September 15, 2024, 01:47:44 pm »
I learned last week you can also run native javascript, if you hate yourself again an evenening you can play with that (with node.js).

Hundreds of millions of dollars have gone into making JavaScript run fast, starting 15 years ago. Heck, it might be billions of dollars by now, between Google, Apple, and Mozilla.

If you run JavaScript in node.js it's faster than C compiled with default optimisation.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #83 on: September 15, 2024, 02:05:04 pm »
How do you mean "run JavaScript in node.js" ?
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #84 on: September 15, 2024, 02:29:33 pm »
How do you mean "run JavaScript in node.js" ?

Code: [Select]
bruce@i9:~$ echo 'function fib(n){return n<2 ? n : fib(n-1)+fib(n-2)}; console.log("Hi from js, fib(40)="+fib(40))' | time node
Hi from js, fib(40)=102334155
0.57user 0.00system 0:00.58elapsed 100%CPU (0avgtext+0avgdata 37872maxresident)k
0inputs+0outputs (0major+2753minor)pagefaults 0swaps

Actually, I exaggerated. C with default optimisation takes 0.509 seconds for the same thing on the same machine. But it has the advantage that the compile time isn't included. If I time compiling and then running I get 0.53 seconds.

« Last Edit: September 15, 2024, 02:33:11 pm by brucehoult »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #85 on: September 15, 2024, 03:12:19 pm »
uh, no one will quibble over 0.21 seconds for a UI launch.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 13024
  • Country: us
Re: hello world for linux in C++
« Reply #86 on: September 15, 2024, 03:24:35 pm »
My only incentive to look at C++ was that I was getting a bit lost in C with the quite distinct separation between the language and the libraries. Try to read about the libraries and you are told that you need to know the linux interface. Start looking at the linux interface and you are told that you need a working understanding of the libraries. So I concluded that with C++ being more modern and maybe less hippy things would be clearer.

This thing about the libraries is quite the same with C++, but even more so. You cannot write modern C++ without making extensive use of the standard libraries (generally STL, or Standard Template Libraries).

On the C side, it should be less the Linux interface, and more the Posix interface, which is a cross-platform standard. But it is true, that the C libraries are very Unixy in nature.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 13024
  • Country: us
Re: hello world for linux in C++
« Reply #87 on: September 15, 2024, 03:28:12 pm »
So python is an interpreted language. Does this mean it will run slower? I have heared that it can be compiled now. Is this a mash up or is it a serious solution?

For most purposes, execution speed is less important than how quickly can you get a job done, or solve a problem.

You can get an idea of what you can do with Python by watching some of Matthias Wandel's videos. He uses Python for calculation tools, hardware interfacing, and remote monitoring, for example.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #88 on: September 15, 2024, 04:14:47 pm »
My only incentive to look at C++ was that I was getting a bit lost in C with the quite distinct separation between the language and the libraries. Try to read about the libraries and you are told that you need to know the linux interface. Start looking at the linux interface and you are told that you need a working understanding of the libraries. So I concluded that with C++ being more modern and maybe less hippy things would be clearer.


This thing about the libraries is quite the same with C++, but even more so. You cannot write modern C++ without making extensive use of the standard libraries (generally STL, or Standard Template Libraries).

On the C side, it should be less the Linux interface, and more the Posix interface, which is a cross-platform standard. But it is true, that the C libraries are very Unixy in nature.

Sorry I forgot to finish, I was going to say that it felt like C++ had more built in support and that maybe the libraries are less of a mystery. I have only done bare metal micro controller programming so far so it's sort of start again from the other end of the candle.
« Last Edit: September 15, 2024, 04:18:34 pm by Simon »
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #89 on: September 15, 2024, 04:15:10 pm »
So python is an interpreted language. Does this mean it will run slower? I have heared that it can be compiled now. Is this a mash up or is it a serious solution?

For most purposes, execution speed is less important than how quickly can you get a job done, or solve a problem.

For reference, that same fib(40) that takes 0.57s in JavaScript and 0.51 seconds in C takes 22.5s in Python, a factor of 40x.

While you can argue that Python is faster to write code in and has a larger ecosystem of libraries than C, I don't think you can claim the same for Python vs JavaScript.

And pretty much all the headline uses of Python are in fact Python wrappers around C code that does all the heavy lifting.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #90 on: September 15, 2024, 04:19:52 pm »
So I don't understand, if it's all C underneath why can't it be properly compiled?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #91 on: September 15, 2024, 04:21:19 pm »
The slowness of Python does not come from the fact that it is not compiled, its interpreter is extremely fast. The slowness comes from the high level abstractions and the need to allocate memory for all the operations. And its allocator is very efficient, but it is still an allocator.

On the up side, you don't have to worry about integer value ranges, it is all infinite precision by default.

And yes, a lot of AI and other high performance uses are just bindings to C code. So slowness of Python is fine because it is just a scripting language to tie together high performance code.

Also, it is possible to write fast native Python code, but you need to be use specific constructions that result in fewer allocations underneath. This is not hard, but it is still something you have to learn.

This is very similar to Matlab. It is a very fast and highly efficient language, but it gets dog slow as soon as you start doing things that result in memory reallocations and data movement.
« Last Edit: September 15, 2024, 04:26:06 pm by ataradov »
Alex
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 13024
  • Country: us
Re: hello world for linux in C++
« Reply #92 on: September 15, 2024, 04:27:39 pm »
For reference, that same fib(40) that takes 0.57s in JavaScript and 0.51 seconds in C takes 22.5s in Python, a factor of 40x.

While you can argue that Python is faster to write code in and has a larger ecosystem of libraries than C, I don't think you can claim the same for Python vs JavaScript.

Yes, but it's a case of using the right language for the right job. Nobody would seriously think Python is the right language for a simple recursive function benchmark. On the other hand, Python is heavily used for machine learning, AI and related tasks, and this is because of the rich numerical libraries and visualization tools that make such jobs a breeze in that environment.

Quote
And pretty much all the headline uses of Python are in fact Python wrappers around C code that does all the heavy lifting.

Exactly. And why is this a bad thing?

Bear in mind that in other languages, a clever optimizer will remove the recursion and turn the program into a simple loop, in which case that benchmark should execute in microseconds.
« Last Edit: September 15, 2024, 04:29:21 pm by IanB »
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #93 on: September 15, 2024, 05:06:07 pm »
For reference, that same fib(40) that takes 0.57s in JavaScript and 0.51 seconds in C takes 22.5s in Python, a factor of 40x.

While you can argue that Python is faster to write code in and has a larger ecosystem of libraries than C, I don't think you can claim the same for Python vs JavaScript.

Yes, but it's a case of using the right language for the right job. Nobody would seriously think Python is the right language for a simple recursive function benchmark.

A simple recursive implementation of Fibonacci numbers is not the point. That's just so I didn't have to write ten thousand lines of Python for a web forum post.

The point is that as soon as you have a lot of Python code, and it's calling functions and looping and computery stuff like that, and takes an annoying amount of time, you can make your computer seem 40 times faster by writing your program in C or JavaScript instead.

JavaScript in particular is just as fast to write as Python, has non-perverse syntax, and has at least as large an ecosystem of libraries as Python. And you don't have to go "Oh no, my code is too slow, I'd better rewrite half of it in C". Because it's already pretty much as fast as C. JavaScript is not only for web sites.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #94 on: September 15, 2024, 05:10:28 pm »
Bear in mind that in other languages, a clever optimizer will remove the recursion and turn the program into a simple loop, in which case that benchmark should execute in microseconds.

I don't know of any optimiser that can turn double recursion into a loop.

Yes, the the Fibonacci sequence happens to be able to be implemented as an iterative recurrence relation, but that's deeper mathematics specific to that function that doesn't apply in general to other doubly-recursive functions such as, for example, QuickSort.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4561
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: hello world for linux in C++
« Reply #95 on: September 15, 2024, 05:58:00 pm »
By far the best example of slow python is SABnzbd. It's a server application with a web frontend that downloads files from usernet servers.
It works fine for your DSL link, but if you get into gigabit wan speeds of 100MB and up the python tax starts to bottlenek on CPU. Especially with python 2 which was fundamentally unable to run multithreaded due to interpreter lock.
Then you upgrade to NGBGet, which does the same, but is written in native C++.

However, this has the drawbacks: It can only run on NAS boxes where is was cross compiled for. This meant that is might run fine on a x86 synology, but not an ARM synology.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #96 on: September 15, 2024, 06:38:37 pm »
Everything Javascript just goes on about web pages. Is every application therefore run in a browser or what?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #97 on: September 15, 2024, 06:41:40 pm »
No, JS can run as a standalone language independent of the browser.  But a basic search would only find the browser stuff, since it is what majority of people would use.

Look for "node.js" stuff specifically. It is Google's V8 JS engine adopted to run outside of the browser.

But I'm not sure I would want to use it as a general purpose language. JS is still pretty lax with its syntax and libraries for the desktop use are not all there. It is still oriented more towards server side of the web applications.
« Last Edit: September 15, 2024, 06:44:05 pm by ataradov »
Alex
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18885
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: hello world for linux in C++
« Reply #98 on: September 15, 2024, 06:48:13 pm »
Ok so it's still python for now, any recommended books on python?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #99 on: September 15, 2024, 06:56:54 pm »
Not from me, I just googled stuff I needed to do if something was not clear.

But I don't understand the switch to Python. What was wrong with C++? If you need GUI, it will be hard to get cross platform stuff no matter what. And FLTK APIs look basically the same in C++ and Python.
Python abstracts some of the OS stuff, but not all, and you still need to be careful writing cross platform code.

One huge disadvantage of Python is that if you are distributing raw *.py files, you will also need to provide instructions to the customers on how to install all the dependencies. There are packers that would let you package all that, but I have no idea how well they work.

No matter what you pick, you will have to spent time figuring things out and making sure that everything works on all the platforms you want to support. There is not one clear winner here, all possible solutions have upsides and downsides. You just need to pick the ones where downsides do not matter in your case. And to do that, you really need to get a few examples going in a few libraries/languages and see which one you like most.
« Last Edit: September 15, 2024, 06:59:28 pm by ataradov »
Alex
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf