C C/C++ and libsysinfo Raspberry Pi3 B+

Hi community!
I'm a beginner in FreeBSD and C/C++ world and my English is bad :( Sorry.
I have a problem with libsysinfo 0.0.3_2
My Project: I install FreeBSD 13 + prosody on RPi3 this sends reports to my client on a daily basis (cron). It works fine with Python + xmpp + psutil libs for Python.
I was thinking why I should use Python when I can use C/C ++. Because C/C ++ is at home with FreeBSD. In principle everything is ready there without a third-party library.
In future I also want to address GPIO with c / c ++
My OS uname -aFreeBSD 13.0-RELEASE-p4 FreeBSD 13.0-RELEASE-p4
My HWRaspberry Pi3 Model B+
C/C++ Compiler g++11 -vCOLLECT_GCC=g++11
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.0 (FreeBSD Ports Collection)

My code:

C:
#include <stdio.h>
#include <sys/sysinfo.h>

int main ()
{
 const long minute = 60;
 const long hour = minute * 60;
 const long day = hour * 24;
 const double megabyte = 1024 * 1024;
 struct sysinfo (si);
 sysinfo(&si);

 printf ("system uptime : %ld days, %ld:%02ld:%02ld\n",
     si.uptime / day, (si.uptime % day) / hour,
     (si.uptime % hour) / minute, si.uptime % minute);
 printf ("total RAM   : %5.1f MB\n", si.totalram / megabyte);
 printf ("free RAM   : %5.1f MB\n", si.freeram / megabyte);
 printf ("process count : %d\n", si.procs);
 return 0;
}

My error:
Code:
/usr/local/bin/ld: /tmp//cc6mu3AE.o: in function `main':
datetime.cpp:(.text+0x4c): undefined reference to `sysinfo'
collect2: error: ld returned 1 exit status
I wanted to reproduce the error in other OS.
OS 1: Arch Linux AMD64, HW Acer NITRO 5 = No error
OS 2: RaspianOS , HW Raspberry Pi 4 = No error

I know Linux is not FreeBSD !
I thought it was because of the Rpi3, so I installed VM with FreeBSD and got the same error.

Any idea ?
Best regards!
 
In future I also want to address GPIO with c / c ++


 
I try it all in new year and i report it here. Thanks to all and Happy new year!
 
I install it from pkg install libsysinfo
I try to remove it and reinstall whit ports. Thenks!
That's probably not a good thing to do. Mixing ports and packages, for new users, is often a problem.

If you installed with pkg(8), then it should be there.

You must include the library in the command to compile your program, otherwise the linker doesn't know where/what/how.
Please show us the command you're using to compile the program.

I am not sure about your particular package, but what does the output of pkg info -l libsysinfo show?

You can also reinstall the package again just in case something went awry initially.
 
gcc11 /Pfad/file.c
without any parameter....
Screenshot_2021-12-30-23-08-27-597_com.arpaplus.adminhands~2.jpg
 
I was thinking why I should use Python when I can use C/C ++. Because C/C ++ is at home with FreeBSD.
The second argument is a bit non-sensical: C/C++ is no more "at home" in Unix as any other programming language. Admittedly, the kernel is written in it. But as soon as you enter user space, other programming languages are used. For example, since time immemorial Unix (and that includes the BSDs) has shipped with programming languages such as assembly and awk. Awk in particular is not a low-level language, and quite useful.

Why use python? Because for many problems (and depending on the skills and knowledge of the programmer), it can be the correct tool. Not always, but often. Forcing yourself to use C/C++ is like saying "I want to use a hammer for this problem, even though it clearly uses screws." And your second sentence is like saying "The people who created the first buildings used hammers, since all they had was nails, so using a hammer must be in some fashion better".

If the program you are writing doesn't use a lot of CPU power, and you can handle the memory overhead of a garbage-collected and interpreted language, then just use Python, if that's what you're familiar with.

Having said that ... let's assume you need to do this in C/C++ for some good reason. In that case: You seem to be trying to do things the BSD way when in BSD land (that's a variation of the old saying of "when in Rome, do like the Romans). In that case, don't use sysinfo. It's a Linux-ism, and a third-party library. All the information you want can be obtained more directly. For the uptime, mark_j already showed you above. For the amount of RAM and the number of processes, read the source code of vmstat or top, and see how they access it, or search the web.
 
I try to remove it and reinstall whit ports. Thenks!
This will have no effect. The problem is likely that you have to add -L/usr/local/lib to your linker flags.

But, as ralphbsz says, sysinfo is a linuxism. You should avoid it if you want to do things the BSD way.
 
All the information you want can be obtained more directly. For the uptime, mark_j already showed you above. For the amount of RAM and the number of processes, read the source code of vmstat or top, and see how they access it, or search the web.
It is difficult for beginners to understand how this works. Im beginner in C/C++. An example:
C++:
#include <time.h>
#include <iostream>
int main()
{
        struct timespec jetzt;
        struct tm *tmbuf;
        clock_gettime(CLOCK_UPTIME, &jetzt);
        tmbuf = gmtime(&jetzt.tv_sec);
        std::cout<<jetzt.tv_sec/3600<<" Stunden online ..."<<std::endl;
        return 0;
}

It took me a day to understand this.
To understand the source code of the program top I'm not that far yet.
Is there any good link on how to read pages and how I can use them in C / C ++ with examples?
 
I see, you're trying to learn C and C++. I don't know much about beginner resources. I would start with a good textbook, I'm old fashioned this way. About 15 years ago, Stanley Lippman's book "C++ primer" had a good reputation. Also: C and C++ really have become significantly different languages, the way they are idiomatically written today.
 
We used Lippman's book in the C++ class I took some decades ago. I remember liking it, and it's still on my bookshelf, but I haven't looked at it in years.

I remember liking Imperfect C++ Practical Solutions for Real-Life Programming, but that was more than a decade ago now too. C++ changes almost as rapidly as Javascript. I suspect anything printed is likely out of date.

I decided some time ago that keeping up with C++ is one of those games where the only way to win is not to play.

This is a gem I ran into recently when trying to decipher some "modern" C++:
 
Off topic:
My view is C++ was a great language when it was early in its lifetime, circa late '90s. Since then it has become a convoluted mess falling for the current syndrome afflicting a lot of languages: we need more features to do the same thing just in a different way because our users are too stupid to think laterally.:eek:

If the OP wants to seriously write in C++, then you'll be forever chasing the latest new/good/interesting thing.

Surprise, surprise, I'm not a big fan of a language that includes a vast array of user functionality, such as the namespace std::* does, for example.
 
It is difficult for beginners to understand how this works. Im beginner in C/C++. An example:
C++:
#include <time.h>
#include <iostream>
int main()
{
        struct timespec jetzt;
        struct tm *tmbuf;
        clock_gettime(CLOCK_UPTIME, &jetzt);
        tmbuf = gmtime(&jetzt.tv_sec);
        std::cout<<jetzt.tv_sec/3600<<" Stunden online ..."<<std::endl;
        return 0;
}

It took me a day to understand this.
To understand the source code of the program top I'm not that far yet.
Is there any good link on how to read pages and how I can use them in C / C ++ with examples?
You should probably use "using namespace std" rather than std::cout etc. Both are correct. Why have 1 way to do something?
You also mix/max include styles. Apparently C++ thinks it's a good idea to drop the .h from includes, because the language is all about concise typing... 🤪

If you're a complete novice, then yes, this is hard to grasp. The use of addresses/pointers, single return values to the left of a function etc all confuse if you're used to languages like python.
 
Off topic:
My view is C++ was a great language when it was early in its lifetime, circa late '90s. Since then it has become a convoluted mess falling for the current syndrome afflicting a lot of languages: we need more features to do the same thing just in a different way because our users are too stupid to think laterally.:eek:

If the OP wants to seriously write in C++, then you'll be forever chasing the latest new/good/interesting thing.

Surprise, surprise, I'm not a big fan of a language that includes a vast array of user functionality, such as the namespace std::* does, for example.

C++ is still that language what it was, because you can find libraries and APIs that are written under normal OO paradigm without any of the newer feature and glitter such as generics or functional programming. Nobody is forcing user to use these features.

I never ever had any problems converting my C mindset to C++. If you think "object oriented" in C - encapsulating states in structs and providing various methods to operate over them, which is what every C programmer does for almost every meaningful task in C.

Too deep object oriented modelling, especially when used with templates, can quickly become a living hell. That's on the expertise on the one who's doing the data model. If a C person does not know what they're doing they're about to fail hard, same thing applies here.

I am currently using two C++ frameworks that I don't have a C replacement for. One is a physics library the other is a 3D engine. Both are very concisely written and accessible to anyone that has "that late 90s C++ knowledge". And I can always produce a fat static library with exported symbols for C and then link it with my main C code.
 
I'm glad you're a fan. I'm not (obviously). I have to use it sometimes, begrudgingly. It's not a language I would recommend to a novice, because if you think C is complex (so the consensus seems to be), C++ is just a factorial of it.
It was a language I would recommend to a novice 20 years ago, though.
 
It is difficult for beginners to understand how this works. Im beginner in C/C++. An example:
[...]
It took me a day to understand this.
To understand the source code of the program top I'm not that far yet.

I see, you're trying to learn C and C++. I don't know much about beginner resources. I would start with a good textbook, I'm old fashioned this way. About 15 years ago, Stanley Lippman's book "C++ primer" had a good reputation. Also: C and C++ really have become significantly different languages, the way they are idiomatically written today.

C++ is a big language; C isn't that small either.
If you are already familiar with a variety of programming paradigms such as algorithmic/structured programming, OO (Object Oriented) and functional then, you can take off on your C++ journey on this broad basis. (This is less applicable to C because it does not have OO or functional aspects defined in its language.)

Taking actual code and disseminating that in every detail is somewhat like detective work and when you have put in a lot of effort and understand it well this can be satisfying. However, without the knowledge how to look at the details and where to find what you are looking for, your search for answers on the internet and other sources will not be easy. If you intend to use this as your learning strategy, I imagine not the most efficient path on your C/C++ journey when you are a beginner.

Something that cannot be stressed enough: subscribe to a C++ or C language forum/mailing list—adjusted to your experience level. Look and ask around how to proceed. Even with the changing C++ landscape one big advantage you have that almost everything is backwards compatible. Be aware that because these are popular languages that there are also a lot of crap books/authors. There may even be good books/authors that do not match your learning style. Acquiring a good working knowledge of a part of the language (especially C++) in order that you can program with confidence without using advanced features (that might very well result shooting yourself in the foot) would be a good starting point in my view.

If you are starting from scratch without much specific C or C++ knowledge it might be helpful to work through a (big) textbook (as mentioned) and practice a lot. Authors for that and more that come to mind: Bjarne Stroustroup; Stanley Lippman & Josée Lajoie & Barbara Moo; Herb Sutter; Scott Meyers; Nicolas Josuttis; Andrew Koenig; but, do look around and make up your own mind. If you do not like or want to process a big text book in every detail from beginning to end, then be sure you know where to find everything in the book so when trying to work out a code example as you mentioned, you have some knowledge where to look. That will give you a much better start and preparation if you want to ask (detailed) questions online. The established textbooks are also usually available translated if you need a start in your native language but, I would encourage you to switch to English.
 
I'll throw this out as a suggestion for learning C++

1. start with plain C only -
a. flow control,
b. expressions, pointers - including pointers to functions
c. initializers
d. stdio eg. printf and fprintf.
2. accept command line parameters from argc, argv.
3. standard C text functions like strcmp, strtol, etc.
4. file I/O - fread/fwrite and POSIX open/read/write/close.
5. connect a TCP socket or send/receive UDP datagrams
6. mimic OO concepts in C eg. first parameter to each API function is a "this" pointer to some struct.
7. mimic OO virtual functions by adding a function pointer to your struct (poor man's "v-table").
8. write your first C++ class and compare the syntax for 6, 7, and 8 - they're not that different.
9. references (C++ only) vs. pointers vs. instances.
10. std::string - this will get your feet wet using iterators.
11. std::list, std::vector, std::map, streams.

Using templates is much easier than writing them but one small typo can result in a snow storm of error messages. I'd defer learning how to write templates until much later.

12. call a C function - with multiple parameters - from C++ (hint: why won't this link?)
13. std::functional and lambdas (I love this feature).
14. tons more stuff goes here.
15. write your own template classes and functions.
16. the rest of STL and boost.

This isn't comprehensive. I've omitted loads of topics. But - once you've reached step 5 - you have all of FreeBSD at your disposal. Unix sockets, shared memory, semaphores, threads, and more. I think steps 6-8 are key for understanding how OO style is implemented by seeing it done from a non-OO code eg. using a disciplined approach to mimic OO style.

That's my 16 cents on a $20 topic.
 
C++ is still that language what it was, because you can find libraries and APIs that are written under normal OO paradigm without any of the newer feature and glitter such as generics or functional programming. Nobody is forcing user to use these features.
Agreed, but it feels like fighting a losing battle. I'd rather use a language whose community doesn't look down on me for using antiquated techniques and disliking their modern idioms.

Unfortunately modern functional programming is making inroads in Java. I recently ran into something like this at work:
Java:
Map<String, Object> someMap = getMap();

someMap.entrySet().forEach(entry -> {
// Do stuff with entry
});

How is that clearer than this?
Java:
for(Entry<String, Object> entry : someMap.entrySet()) {
// Do stuff with entry
};

You have no indication that entry is actually a key-value pair of String, Object in the functional example. It's obvious the way I've written it, but what if the call to getMap() was dozens of lines ago and hidden in some complex code? (It was in this case). Worse, what do you think will happen if you try something like this?
Java:
boolean foundFoo;
someMap.entrySet().forEach(entry -> {
// Do stuff with entry
    foundFoo = "foo".equals(entry.getKey());
});

That will fail to compile with this error:
local variables referenced from an inner class must be final or effectively final

Huh? What inner class? What does "effectively final" mean?

The compiler actually creates an anonymous class at compile time that will be loaded from disk at initialization time, instantiated every time this code is run, and marked for garbage collection every time the forEach() function returns. Oh, it will also invoke the run() method in this object for every entry in the map.

Functional programing in a nutshell, folks. Doing less with more, and with code that's harder to follow. But you shouldn't listen to me. I'm just a dinosaur that likes antiquated, boring, non-functional for loops.
 
The following are all my personal opinions ... everyone has a different style of learning.

At this point in time, I think the best investment of time is going for learning up-to-date C++, meaning versions 17 and 20. That's what commercial code is written in, and the language looks very different (massive use of auto variables, various forms of protected pointers, templates everywhere, lots of functional-style programming).

If you are starting from scratch without much specific C or C++ knowledge it might be helpful to work through a (big) textbook (as mentioned) and practice a lot. Authors for that and more that come to mind: Bjarne Stroustroup; Stanley Lippman & Josée Lajoie & Barbara Moo; Herb Sutter; Scott Meyers; Nicolas Josuttis; Andrew Koenig; but, do look around and make up your own mind.
  • Stroustrup's book is awful. I've tried several times to read it (at a time when I was good at C++, which was around 15 years ago), and his explanations make no sense to me. I find it obnoxious and unintelligible. It keeps getting updated.
  • The Lippman et al. book is good. I actually had that at my desk, as a reference. Newer editions exist.
  • Sutter's C++ style book was good, but by now it is ancient. Note that I'm referring to this "coding style guide" book, not his core C++ book. I don't know whether he has written any updates to any of his books.
  • Scott Meyers' "Effective..." books were absolutely wonderful, super useful. Not as a references, but as a teaching tool: Do these 35 or 50 things, and you avoid lots of problems. I know that there is a new edition of his book out, which brings it up I think to the C++14 standard.
  • Andy Koenig's book is decent: Intelligible. But as a learning tool, not so good.
  • Don't know Josuttis.
  • I happen to know that someone is writing a new C++ book right now, and the interesting part is that they're writing a pair of matching programming textbooks, in Python and in C++. I think that will be very useful for people who want to transition from Python to C++. And given the schedule, the C++ book will be up-to-date (including the C++17 and C++20 standards). But the two books are not out yet, probably 3 or 6 more months until they are available.
 
Back
Top