Author Topic: hello world for linux in C++  (Read 27322 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 #100 on: September 15, 2024, 07:17:44 pm »
Well apparently you can do it all in python, like you say I just need to get something going. I guess I need to try C++ too but it looks like maybe I just need to code for linux on Linux. I just go round in circles trying to setup an environment to work in. It's like they said just drive down the road in this car but the car has the wheels laying on the floor and the wrong size nuts are there to put them back on with and the only shop in town that sells them shut for a month.

I'm almost sick of linux before I even start. Clearly I have to be a commandh line guru or I should not do anything. I set up debian on a laptop as I could not get anything sensible to run on a rasperry pi, well ubuntu would not even work on this laptop but then I think ubuntu is the windows of the linux world. So in debian I can't even copy and paste files from one folder to another and many other "normal" things you would do work as even though my account is an admin one, nah, I mean how hard is it supposed to be?

Is a virtual machine worth considering? save me from jumping between a nice large desktop and a poxy little laptop. every time I look something up for help I just find more gogglybook, I mean where the heck does one start when one wants to start? No tutorial explains why you should do what it says and no tutorial is successful, but as they never told you why they told you to do things you are left, once again, 90% of the way there and buggered.
 

Offline ralphrmartin

  • Frequent Contributor
  • **
  • Posts: 517
  • Country: gb
    • Me
Re: hello world for linux in C++
« Reply #101 on: September 15, 2024, 07:22:18 pm »
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.

You need a language that will do memoisation, like LISP... >:D >:D
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 13024
  • Country: us
Re: hello world for linux in C++
« Reply #102 on: September 15, 2024, 07:31:44 pm »
Is a virtual machine worth considering? save me from jumping between a nice large desktop and a poxy little laptop. every time I look something up for help I just find more gogglybook, I mean where the heck does one start when one wants to start? No tutorial explains why you should do what it says and no tutorial is successful, but as they never told you why they told you to do things you are left, once again, 90% of the way there and buggered.

Yes, for sure, a VM is worth considering. But before you go there, did you know Windows can run Linux directly, without even switching to a different machine?

https://learn.microsoft.com/en-us/windows/wsl/install

Windows also has a VM environment built in, and you can run Linux easily there too:

https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/about/

But, for a good experience you will need to make sure you have a decent spec PC with plenty of cores and memory, and you may need to be running Windows 10 Professional or Windows 11.
« Last Edit: September 15, 2024, 07:34:49 pm by IanB »
 

Offline Postal2

  • Frequent Contributor
  • **
  • !
  • Posts: 826
  • Country: 00
Re: hello world for linux in C++
« Reply #103 on: September 15, 2024, 07:50:55 pm »
...I guess I need to try C++ ...
You don't need to "try C++" specifically, this extension is specifically made for the environment where you just write C. Over time, your eye will get used to the syntax generated by the machine, and you will start using it.
 

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 #104 on: September 15, 2024, 08:02:57 pm »
Well I have installed code blocks on my debian laptop and the hello world fltk program runs. vs code could not find any of the includes, despite me following the instructions.
 

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 #105 on: September 15, 2024, 08:08:15 pm »
Is a virtual machine worth considering? save me from jumping between a nice large desktop and a poxy little laptop. every time I look something up for help I just find more gogglybook, I mean where the heck does one start when one wants to start? No tutorial explains why you should do what it says and no tutorial is successful, but as they never told you why they told you to do things you are left, once again, 90% of the way there and buggered.

Yes, for sure, a VM is worth considering. But before you go there, did you know Windows can run Linux directly, without even switching to a different machine?

https://learn.microsoft.com/en-us/windows/wsl/install

Windows also has a VM environment built in, and you can run Linux easily there too:

https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/about/

But, for a good experience you will need to make sure you have a decent spec PC with plenty of cores and memory, and you may need to be running Windows 10 Professional or Windows 11.


Yes I have heard of it, I have probably heard of most things by now but hearing of them is not enough to get anywhere with them. I started this thread using visual studio on a computer that had the WSL installed (and that name is backwards.... it's a linux subsystem that runs on windows? but then I know nothing). But I could not connect to it! I did manage to connect my physical laptop. But then the FLTK header files could not be found despite me following the install instructions that naturally were written for people who already know how the whole shbang works and as everyone does this I have no way of know where to start and how a basic setup works.
 

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 #106 on: September 15, 2024, 08:10:40 pm »
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.

You need a language that will do memoisation, like LISP... >:D >:D

I already have a lisp and it never got me anywhere :D >:D
 

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 #107 on: September 15, 2024, 08:12:01 pm »
my virtiual machine also with debian has decided that my user is not on the sudo list...... cause every time I install the very same thing I get different results. Is ubuntu any friendlier? will it work on a virtual machine?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #108 on: September 15, 2024, 08:19:41 pm »
You can add user to the sudo list. What's the problem? Also what different results? Any Linux distro will work on a VM. But if you just jump from one to the other at  the slightest problem you will not get anywhere. Just stick with one and figure out how to use it.

But if you are asking for recommendations, I would stick with Mint.
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 #109 on: September 15, 2024, 08:34:23 pm »
Yea I came across some long winded explanation. But that seems to be linux, sure you can drive down the road today, just change the brakes first... because.... I might try mint, I did try it once for desktop but as I'm sure we all know, unless you can program for linux you have no business expecting to be able to use it day to day so I'm still on windows. If I manage to get up and running with programming I guess I'll be able to use it properly some day.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #110 on: September 15, 2024, 08:51:00 pm »
I'm sure we all know, unless you can program for linux you have no business expecting to be able to use it day to day
This is blatantly not true. There is absolutely no need to know programming to use Linux.
Alex
 
The following users thanked this post: nctnico

Online IanB

  • Super Contributor
  • ***
  • Posts: 13024
  • Country: us
Re: hello world for linux in C++
« Reply #111 on: September 15, 2024, 09:17:51 pm »
Yes I have heard of it, I have probably heard of most things by now but hearing of them is not enough to get anywhere with them. I started this thread using visual studio on a computer that had the WSL installed (and that name is backwards.... it's a linux subsystem that runs on windows? but then I know nothing). But I could not connect to it! I did manage to connect my physical laptop. But then the FLTK header files could not be found despite me following the install instructions that naturally were written for people who already know how the whole shbang works and as everyone does this I have no way of know where to start and how a basic setup works.

Yes, but Visual Studio is for Windows, not for Linux. If you try to build Linux programs on Visual Studio you will be making things impossibly difficult. To work on Linux, you have to work entirely on Linux, and ignore Windows. That means using Linux code editors, and Linux compilers, like gcc.

You can install and run VS Code on Linux, but not Visual Studio. I know we've been here before, but it is worth repeating: VS Code is not Visual Studio, and Visual Studio is not VS Code. They have got nothing to do with each other. When posting here, please do not say "Visual Studio" unless you truly, genuinely mean Visual Studio, and not VS Code. Otherwise we will be very confused.

This is the page about how to install VS Code on Linux:

https://code.visualstudio.com/docs/setup/linux

I know the title says "Visual Studio Code", but that is insanely stupid on Microsoft's part, and it is just a case of Microsoft trying to confuse people.
 
The following users thanked this post: shabaz

Offline shabaz

  • Super Contributor
  • ***
  • Posts: 1011
Re: hello world for linux in C++
« Reply #112 on: September 15, 2024, 11:43:02 pm »
Some of it is just about finding a way that you're comfortable with, there's no one right way or wrong way.
For instance, I do most of my development on a Windows machine, but there are so many things that I'd like to also do in Linux, so a VM is super-useful. However, since I'm usually in the same physical location where I can have other machines, I decided to get some cheap machines (I have two or three Intel NUCs, they are tiny, fairly quiet, and cost very little nowadays in used condition, maybe $100 or so, i.e. cheaper than some Raspberry Pi's + enclosure + SSD), and then I've got Linux on a couple of them, and VMWare ESXI (it is free, bit of a learning curve though, and not an essential thing to start off with) on another so that I can quickly swap between VMs (i.e. I only run one VM on that box usually, but could run a couple simultaneously if really required). None of the NUCs are connected to a monitor or keyboard, they are just connected via WiFi or Ethernet to the network.

VS Code on Windows has really nice remote Linux coding capability (it is an extension from Microsoft called "Remote - SSH"), it is pretty exceptional. Well worth a try in case it suits the way you want to work. This saves me from needing to run a remote desktop. Then, I can use an SSH terminal (I like
SecureCRT on Windows, it is equivalent to free PuTTY, but nicer but not free; there are other good free options but I don't know them) to get to a Linux shell and perform builds there if desired. Also, totally agree with the above comment that if you're developing for Linux, then build it on Linux, i.e. don't cross-compile unless you can justify it to yourself, because it is easy to end up with things breaking due to slightly different libraries being used. Cross-compiling is necessary for microcontrollers, but for desktop and server apps it's unnecessary usually (unless you're developing for a career, in which case there could be very good reasons for it, e.g. build-server support by the IT team, higher performance machines, and so on).

I developed a C++ application running on a server for about five years, and it takes daily work with C++ to try to be reasonably competent, so it is for sure a high barrier. I'm still only familiar with a subset of C++ and a subset of the Standard Template Libraries (STL), since I no longer code frequently in C++. I didn't like it initially, but now I prefer Python unless there's a good reason not to. It's just very productive, you can do a lot with very few lines of code, and it is excellent for data manipulation type of programs. Plus there are many libraries for helping with almost anything. Example: I needed to write code the other day to make backups of file folders, timestamped and delete the oldest, and search-and-replace content within each one, based on text menu input from the user; it was all done in a couple of dozen lines of code.

Python is a good complement to more 'normal' languages like C/C++; for instance, for writing tests, Python can save a lot of time, because you can use it interactively. In other words, your microcontroller app could be in say C or C++, but you could then test it with Python from your PC (or from microcontrollers running MicroPython). As an example, you could write a function to send I2C data, and then interactively (from a Python shell) issue commands one at a time, allowing you to (say) capture other signals with a 'scope easily since you're in control of the I2C stream. This is just a contrived example. Plus, MicroPython on boards like Pi Pico is very close to normal Python, so you've got control of hardware interfaces (GPIO, and peripheral interfaces like SPI or I2C, or UART or RS-232) very easily, under direct interactive control.

As an example, when I wanted to test a servo motor, the interactivity looked something like this, all done one command at a time, so I could (say) quickly stop the motor if I was unhappy with something, or go back and try a different speed if it vibrated and so on:

Code: [Select]
import my_motor_controller as m
m.reset()
m.op_mode(m.SPEED)
m.set_velocity(20)
m.set_velocity(400)

Some systems allow for combining great speed with that Python ease-of-use. An obvious example is that Pi Pico's high-speed 'PIO' code can be executed from MicroPython and data from the PIO can be processed by MicroPython too. You can also technically write some function in C to accelerate them, and execute from within MicroPython, although there are some limitations (it suits algorithms more than GPIO control from C).

On a PC, you could write some parts of code architected as C command-line apps, and then execute them from within Python, but I've not often needed to do that, since Python is speedy enough for whatever I've worked on.

Regarding GUIs, unfortunately (or fortunately) I've never had much need for developing them, and so I use what tools I'm familiar with, which can be config files, or command-line parameters, or (if none are supplied) just get the C/C++ or Python program to display a menu and prompt the user for input. But as mentioned by others, there are Python GUI libraries; I've not used a single library frequently enough to get good with it, but pyQt was fairly straightforward to use the few times I've tried it.

If I just need graphical output with no GUI interaction, then again there are lots of options, ranging from (say) matplotlib for nice charts, to writing out SVG format, or generating data files for importing and charting with Excel or MATLAB or whatever.

I also quite like (hate to mention the language!) to just use JavaScript for a simple web app; I don't like JavaScript itself, but it sure is convenient for a way to make an app that needs no compiling and will run on any platform with a browser. It's also super-easy to then convert that to what looks like an app on your mobile phone. I'm just a beginner, but there are enough examples online to make progress with JavaScript to do quite a lot. JavaScript can interact with hardware through various ways; one way is to implement your own web server (this is where Node.js is quite a useful option (it runs JavaScript on your server too, so you can program exclusively in JavaScript on both server and client [browser page], but you can also use Python, if you wish to minimize JavaScript usage to the web page only.

A nice mechanism (well, it's the only one I know - I'm no expert in this) is to use socket.io; it allows you to asynchronously send and receive data between the server and the web page in the browser. This means you don't really need a GUI library at all, if all you want is things like boxes and buttons on a browser page, interacting with your app.

I'm OK with not knowing everything, and not starting from first principles when it comes to writing code; I can generally fill in the blanks later for the areas I didn't initially understand. This doesn't suit everyone of course. I spend a fair bit on books (and books get outdated quickly, but I still find it's worth the investment provided I make use of the book quickly!). Provided it is a good book of course (some are pretty bad) to learn some best practices etc. Watching programmers code over their shoulder, to see what tools they use, how they name things, what notes they take and so on, and ask them questions, is super-interesting, but it's not feasible for many, especially if you're working in isolation.

I'm a member of a Linux User Group or LUG (I simply joined an arbitrary one, not actually anywhere near me) and if I get stuck, then I can email and someone might know the answer, or I can wait and ask questions on the next Webex/Zoom group meeting and so on. A nearby one would have the benefit of in-person meetings of course. Many members do programming for a career, so generally someone is around to give some help if it comes to it; I don't necessarily ask Linux-specific questions, I just ask general programming questions and I try not to annoy the members by focusing on Windows. There are LUGs worldwide.

This comment was a longer than it was intended to be; I don't know if any is useful or relevant, but I thought it worth adding in case you spot anything useful.

« Last Edit: September 15, 2024, 11:50:07 pm by shabaz »
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #113 on: September 16, 2024, 12:38:06 am »
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.

You need a language that will do memoisation, like LISP... >:D >:D

Any language can do memoisation using an array or hash table / dictionary but yes only a few such as CLOS, Dylan, Prolog allow you to add methods to Generic Functions on the fly.

For example, consider the following Dylan program:

Code: [Select]
Module: hello-world

define dynamic generic fib (n :: <integer>);

define method fib (n == 0) 0 end;
define method fib (n == 1) 1 end;

define method fib (n :: <integer>)
  let res = fib(n - 1) + fib(n - 2);
  //add-method(fib, method(n == n) res end);
  res
end;

format-out("Hello, world! %=\n", fib(40));

Compiled with Open Dylan 2024.1 it runs in 1.655 seconds.

Uncommenting the add-method line makes it run in 0.013 seconds.

Writing a normal recursive function makes it run in 0.39s, so faster than the 0.51s for C with default -O0 optimisation.

Code: [Select]
define function fib (n :: <integer>) => (res :: <integer>)
  if (n < 2)
    n
  else
    fib(n - 1) + fib(n - 2)
  end
end;

Generic functions are a little bit slow compared to simple function calls, as they have to examine the arguments to do dynamic dispatch. But of course you would not normally do this for such simple functions, so the overhead would be less.
« Last Edit: September 16, 2024, 06:59:33 am 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 #114 on: September 16, 2024, 06:29:16 am »
Yes I have heard of it, I have probably heard of most things by now but hearing of them is not enough to get anywhere with them. I started this thread using visual studio on a computer that had the WSL installed (and that name is backwards.... it's a linux subsystem that runs on windows? but then I know nothing). But I could not connect to it! I did manage to connect my physical laptop. But then the FLTK header files could not be found despite me following the install instructions that naturally were written for people who already know how the whole shbang works and as everyone does this I have no way of know where to start and how a basic setup works.

Yes, but Visual Studio is for Windows, not for Linux. If you try to build Linux programs on Visual Studio you will be making things impossibly difficult. To work on Linux, you have to work entirely on Linux, and ignore Windows. That means using Linux code editors, and Linux compilers, like gcc.

You can install and run VS Code on Linux, but not Visual Studio. I know we've been here before, but it is worth repeating: VS Code is not Visual Studio, and Visual Studio is not VS Code. They have got nothing to do with each other. When posting here, please do not say "Visual Studio" unless you truly, genuinely mean Visual Studio, and not VS Code. Otherwise we will be very confused.

This is the page about how to install VS Code on Linux:

https://code.visualstudio.com/docs/setup/linux

I know the title says "Visual Studio Code", but that is insanely stupid on Microsoft's part, and it is just a case of Microsoft trying to confuse people.

Indeed, when I said visual studio I meant visual studio, and when I said visual studio code I meant visual studio code. I am aware that they are not the same thing. I have to try everything. I prefer visual studio, but alas. as vs code is just an editor it makes things more complicated as setting up paths etc is left to me.
 

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 #115 on: September 16, 2024, 06:38:04 am »
I'm sure we all know, unless you can program for linux you have no business expecting to be able to use it day to day
This is blatantly not true. There is absolutely no need to know programming to use Linux.

In the past every time I sought help the answer was, well this is linux, we fix our own problems, that is what makes it so great you can fix the code and change things because it's open source. I kid you not, I have been told this when reporting problems.

I once installed the open source equivalent of cPanel on a server. Got it all setup and a little bemused by the minimal user interface. Then the emails stopped working after an update. I spent ages just finding out the problem with a well known package. Everyone seemed to know and accept that it was broken. Small matter of a line of text in some file if not a character that someone grudgingly told me about. Now cPanel uses the same package, never had the problem, but cPanel is commercial with customers to keep happy, not a bunch of hippies that measure people up by how many well known but unfixed bugs they know about.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 5730
  • Country: Earth
Re: hello world for linux in C++
« Reply #116 on: September 16, 2024, 06:38:51 am »
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.

JS could be a great language, but...

 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #117 on: September 16, 2024, 06:47:24 am »
In the past every time I sought help the answer was, well this is linux, we fix our own problems, that is what makes it so great you can fix the code and change things because it's open source. I kid you not, I have been told this when reporting problems.
This is because instead of asking specific questions you make statements like "I've got some error, your stuff is broken".

All software has bugs. For closed source software people just came to accept them because there is not much you can do. OSS does offer you ability to fix bugs, but you are absolutely not expected to do so. You can just do the closed source thing and wait for someone else to fix it. And if some piece of software does not work for you as a result of existing bugs, you can try something else. There is not just one cPanel equivalent, there are a lot. If all of them don't work, you are likely doing something wrong.

But if you think Linux if not for you, it is fine. At the same time if you really think it is broken beyond being usable, you are mistaken and likely missing something. Whether you want to figure it out and use it is up to you.
Alex
 

Offline gnif

  • Administrator
  • *****
  • Posts: 1930
  • Country: au
  • Views and opinions are my own
Re: hello world for linux in C++
« Reply #118 on: September 16, 2024, 07:44:05 am »
@Simon

I have not read through every post here, but I will weigh in with my $0.02 on development on Linux. But first a few things to clear up.

C and C++ can link against and use each others libraries. If your only goal is to use C++ so you can use C++ libraries, then you're already shooting yourself in the foot. The main difference is the function name mangling, C doesn't mangle names, but C++ does. You can though write wrapper headers that will interface to C++ code from C just fine.

Note: this is all written from the top of my head, there may be some errors here.

MyClass.h
Code: [Select]
#ifdef __cplusplus

class MyClass
{
public:
  MyClass();
  ~MyClass();
};

extern "C" {
#endif

typedef void * MyClassHandle;

MyClassHandle createClassFromC();
void callClassMethodFromC(MyClassHandle handle);
void freeClassFromC(MyClassHandle handle);

#ifdef __cplusplus
}
#endif

The "extern "C" {" directive tells the C++ compiler to use non-mangled C compatible function names and calling convention.

MyClass.cpp
Code: [Select]
#include "MyClass.h"

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

MyClassHandle createClassFromC()
{
  return (MyClassHandle *)(new MyClass());
}

void callClassMethodFromC(MyClassHandle handle)
{
  ((MyClass * )handle)->calledFromC();
}

void freeClassFromC(MyClassHandle handle)
{
  delete (MyClass *)handle;
}

main.c
Code: [Select]
#include "MyClass.h"

int main(int argv, char * argv[])
{
  MyClassHandle handle = createClass();
  callableFromC(handle);
  freeClass(handle);
}

Compiling this is simple, build `cpp` sources with `g++` and `c` sources with `gcc`, but do not link.
Code: [Select]
g++ -c -o MyClass.o MyClass.cpp
gcc -c -o main.o main.c

Then link the final executable using the linker (LD) to create the final executable.
Code: [Select]
ld -o main MyClass.o main.o

Next up is automating much of this, on Linux `make` is the defacto standard, but there are alternatives or helpers such as `automake/autoconf`. I personally prefer `CMake`. But lets take a step back and just use `make` to compile the above example:

Makefile:
Code: [Select]
CC=gcc
CXX=g++
LD=ld

myApp:
  $CXX -c -o MyClass.o MyClass.cpp
  $CC -c o main.o main.c
  $LD -o myApp MyClass.o main.o

all: myApp

This is as simple as you can get but it would require a fair bit of manual work to maintain a larger project. Do some research, there are good guides on how to use `make` effectively. To compile your projects, all you then need to do is run `make` and it would result in building the `myApp` file if it was not already built.

There are helpers on Linux for linking, instead of specifying the library directly should rather use the `pkgconfig` tool. For example, to use a library like `libpng` you also need to amend the compiler (gcc/g++) flags with the include path directive, which can change depending on the distro/platform you build on.

for example:

Makefile
Code: [Select]
CC=gcc
CXX=g++
LD=ld

CFLAGS=`pkgconf --cflags libpng`
LDFLAGS=`pkgconf --libs libpng`

myApp:
  $CC -c -o main.o $CFLAGS main.c
  $LD -o myApp main.o $LDFLAGS

all: myApp

This would expand to:
Code: [Select]
gcc -c o main.o -I/usr/include/libpng16 main.c
ld -o myApp main.o -lpng16

Learning how all this works is critical to building projects that will build anywhere on anything. For example, the Looking Glass project (https://looking-glass.io) builds two applications, one is for Linux, the other is for Windows and can be either compiled native directly on windows, or cross compiled for windows on Linux, all with no code changes. (It also uses the C++ Dear ImGUI library from C via the cimgui api-wrapper https://github.com/cimgui/cimgui, and the C++ only Windows NvFBC capture interface by means of a some helper functions)

I hope I was clear here, if there is anything more I can do to answer your questions please feel free to ask me (and tag me or I might miss it).
« Last Edit: September 16, 2024, 07:56:16 am by gnif »
 

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 #119 on: September 16, 2024, 07:55:20 am »
I am not saying that linux is broken, the world around us is a testament to that. But it often feels like you are on your own. Things are naturally complex but when seeking help I seem to hit the sort of attitude we may treat someone with who persists in being told which side of an incandescent light is the negative or positive. Or maybe a better comparison is the AVR freaks forum assuming it has not changed it's culture. There are people who are just not interested in the simple stuff and just expect you to know it.

As I said when I try to look up official documentation it has a prerequisite that I am familiar with the documentation of other features/software, but on checking this documentation I find that either it points me right back where I started as a prerequisite or has a prerequisite for something else. As in my example with the linux interface and the C libraries, both expect knowledge of each other.

I guess that this is why the culture of the copy and paste programmer has arisen. I don't like just doing something because a web page tells me. We have an entire product that a now departed colleague designed in this way. As a result any change meant weeks of more copy paste research because he had no clue about how much of what he had done really worked, he would just shrug his shoulders and say I don't know when I asked when trying to help him by just getting him to "rubber duck" the situation.

it is this product that I am now in a way trying to gain the skills to replace. I realize that programming takes years to learn properly despite a book on amazon claiming that you can learn python in a week and unlock huge salaries. but I seem to spend a lot of time just trying to make a start with the fundamentals.

I prefer to read a decent book, but most of what is produced today is for people with no patience.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6398
  • Country: nz
Re: hello world for linux in C++
« Reply #120 on: September 16, 2024, 09:01:31 am »
As I said when I try to look up official documentation it has a prerequisite that I am familiar with the documentation of other features/software, but on checking this documentation I find that either it points me right back where I started as a prerequisite or has a prerequisite for something else. As in my example with the linux interface and the C libraries, both expect knowledge of each other.

That is not correct.

The Linux syscall interface is strictly stand-alone. The calls are described in terms of being called from the C language but NOT using any knowledge of the C standard library.

e.g. man 2 write

Code: [Select]
WRITE(2)                                 Linux Programmer's Manual                                WRITE(2)

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);

DESCRIPTION
       write()  writes  up  to  count bytes from the buffer starting at buf to the file referred to by the
       file descriptor fd.

<etc>

C library functions build on top of whatever the OS provides e.g. fwrite(), which in this case on Linux is write() but on Windows or classic MacOS is something else. But C library functions such as printf() build on top of fwrite() without corresponding in any way to what the OS provides.

Again, nothing in the Linux syscall interface ever depends in any way on the C standard library. The calls are described in terms of C, but you can equally well write them directly in assembly language with no library needed. Either way, whether you write in C or assembly language, if you only use Linux syscalls then you don't even link with the C library, you can say -nostdlib to gcc/clang.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #121 on: September 16, 2024, 01:49:32 pm »
I am not saying that linux is broken, the world around us is a testament to that. But it often feels like you are on your own.
This is going to happen to anything that is "not windows". if feels like this because you don't know the correct terms to google and correct way to ask questions and what information to incude in the question.

Things are naturally complex but when seeking help I seem to hit the sort of attitude we may treat someone with who persists in being told which side of an incandescent light is the negative or positive.
This is because people asking question often approach it from the position "this is not how it was in windows, I want things to be like in windows". Same things happens when people move from Altium to KiCad. They often complain that KiCad does not work like Altium without taking tie and effort to understand how things work.
Alex
 
The following users thanked this post: nctnico, Nominal Animal

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 #122 on: September 16, 2024, 05:12:14 pm »

Again, nothing in the Linux syscall interface ever depends in any way on the C standard library. The calls are described in terms of C, but you can equally well write them directly in assembly language with no library needed. Either way, whether you write in C or assembly language, if you only use Linux syscalls then you don't even link with the C library, you can say -nostdlib to gcc/clang.


I've never heard of a syscall interface. So what sorts of things do they do and where am I calling them from? if I write a program it is in "a" language, I can't just type anything into my program.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12463
  • Country: us
    • Personal site
Re: hello world for linux in C++
« Reply #123 on: September 16, 2024, 05:27:20 pm »
Your language needs to have access to the low level instructions. This is hardware dependent. On x86 syscalls are called though the interrupt 0x80, on x64 there is a special instruction called "syscall". The arguments to the called function are passed in the registers. There are many sources for the syscall table, here is one https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/

All low level library functions like read() or write() are just simple interfaces for those syscalls. You can do it all manually if you want to or if your language does not already have this list translated into a native to the language form.

Here is a basic assembly example that prints "Hello World!" without any libraries - https://www.cs.fsu.edu/~langley/CNT5605/2017-Summer/assembly-example/assembly.html
« Last Edit: September 16, 2024, 05:38:32 pm by ataradov »
Alex
 

Offline Postal2

  • Frequent Contributor
  • **
  • !
  • Posts: 826
  • Country: 00
Re: hello world for linux in C++
« Reply #124 on: September 16, 2024, 06:26:18 pm »
... You can do it all manually ....
Why assembler, write everything in processor codes. I wrote a floppy disk driver in x86 codes. I keep this paper notebook and can take a photo if anyone doesn't believe me. So give me the Biggest Fool Award.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf