How do I get hardware information on Linux/Unix? - c++

How I can get hardware information from a Linux / Unix machine.
Is there a set of APIs?
I am trying to get information like:
OS name.
OS version.
available network adapters.
information on network adapters.
all the installed software.
I am looking for an application which collects this information and show it in a nice format.
I have used something similar with the "system_profile" command line tool for Mac OS X. I
was wondering if something similar is available for Linux as well.

If you need a simple answer, use:
cat /proc/cpuinfo
cat /proc/meminfo
lspci
lsusb
and harvest any info you need from the output of these commands. (Note: the cut command may be your friend here if you are writing a shell script.)
Should you need more detail, add a -v switch to get verbose output from the lspci and lsusb commands.
If what you are looking for is a more feature-complete API, then use HAL, though that may be an overkill for what you are trying to build.

If you are looking for a tool that show System Information, the GUI tool like HardInfo would useful for you.
In Ubuntu, you can install HardInfo like this...
sudo apt-get install hardinfo
Cheers

There is a bash command lshw - list hardware

I would use hal, the hardware abstraction layer. It includes both some GUI commands, some tty commands (which can be used from shell programs), and library bindings for c and multiple other languages.
HAL is not really a standard part of "linux", but I think it is used by most modern distros.

Try sudo lshw.
It's the easiest.

Since you mentioned API, try the exec family of commands for C. You can use them to execute these binaries that other people have mentioned. To create a robust/flexible solution you will probably also have to leverage the Unix fork() commands. You will also have to develop a mechanism for capturing the output spewed by these utilities. Look into Unix pipes for that.

You can use inxi which provide all hardware information including cpu temperature and so on.
Install on Red Hat based OS
sudo dnf install inixi
Install on Debian based OS
apt-get install inxi

Related

Is there an equivalent of python's virtualenv for C/C++ programs?

I have to develop C++ programs that have to run on Scientific Linux 5 or 6. I would like to develop with QtCreator on Ubuntu which has much more recent libraries than the one found on SCL6.
Is there an equivalent of Python's virtualenv for C/C++ programs ?
Using a chroot with all the required libraries and dependencies could do the job. Does this exist ? See http://rcrowley.org/articles/dependencies.html on the use of chroot.
Use debootstrap to create the chroot environment (or even install ubuntu on a separate partition). Mount your home dir with mount -o bind. Use schroot convenient chroot setup.
http://manpages.ubuntu.com/manpages/precise/en/man8/debootstrap.8.html
http://manpages.ubuntu.com/manpages/precise/en/man8/mount.8.html
http://manpages.ubuntu.com/manpages/precise/en/man1/schroot.1.html
You can use tools below:
conan virtual environment (https://blog.conan.io/2016/08/04/Conan-virtual-environments-Manage-your-C-and-C++-tools.html)
conda virtual environment (https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)
docker container
Not sure it is lightweight enough for what you need (I'm not very familiar with virtualenv) but you can try the CDE Project which is a very nice way of creating a virtual sandbox with all kinds of dependencies.
You can establish the dependencies and the compiler for a given project using a build system like bazel (https://bazel.build/) or please (https://please.build/).
It will never be the same exact as a virtualenv, due to the different nature of the language, and since it will still be using the system compiler. In case you want to have your project completely isolated, you can ship the project on a docker container.
I agree with SeF using dependencies. For me, the IDE helped to set different development environment.
Regarding the IDE for different OS:
Linux: Ecplise
Windows: Visual Studio

Learning Linux from Windows Newbie questions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am a newbie in linux and programming. I want to learn linux command and use create some C and C++ programme to interact with the linux API from my windows XP. However, I am not sure how to set up the environment from my windows based computer.
What programme should I install? Also, it seems like linux has Ubuntu, Fedora. I heard of Red Hat as well. What is the difference and which one should I install?
Also, is there any difference between using linux with user interface like Ubuntu, using an IDE to create programme AND the command line terminal using VIM to create programme?
Besides, I have heard of using Valgrind to debug programme. Does Valgrind works together with an IDE in Ubuntu or works in command line terminal only? If my IDE already has debugger, do I still need Valgrind?
Sorry for such newbie questions.
Thanks.
Your question is very vague and prone to start argumentations and fights. Also, you miss-used many terminologies there and before you even start programming with Linux, you should first get acquainted with the OS, especially the terminal... But first things first, programming in C/C++ for Windows is not entirely the same as programming in C/C++ for Linux. If you want the latter, then use the latter.
My suggestion is this :
Grab VirtualBox and install it.
Download Ubuntu ; IMHO, Ubuntu is best for starters (or anyone as a matter of fact) because it has a lot of support, a good user base and is compatible with pretty much any Linux software installer (RPM, deb, etc.) You can choose any other distribution, it doesn't really matter, but I recommend this one. [1]
Start VirtualBox and create a new Ubuntu virtual machine. The steps are pretty straight forward, consult the documentation for any assistance). Your virtual machine may look something like :
1GB of RAM will be enough;
10GB of hard disk (you won't need much more, but you may increase the size if you think you'll need more space for /home, see next point)
a network adapter set a bridged
etc.
Install Ubuntu from the ISO that you have just downloaded (that you have mounted into VirtualBox as a CD-ROM device) You'll only need about 8GB of hard disk total for a typical, minimum Ubuntu installation (ext4+swap), however I recommend this setup.
Enjoy your installation. (Tip: now you can install the VirtualBox's Guest Additions.)
Open a terminal in your Ubuntu VirtualBox window and type sudo apt-get install build-essential to install the GCC compiler
Gedit is already installed by default with Ubuntu and it's a fairly good text editor compared to Windows' notepad. However, vim is not, but you can install it with sudo apt-get install vim in the terminal.
And voilà! You're all set to go to do some C/C++ programming in a Linux environment, where you can still have Windows in case you're stuck.
I also recommend you do most of your learning using the terminal (aka the command line) so you know how things work under the hood. Then, when you are familiar with the GCC compilier, MAKEFILEs, etc. you can install some IDE to avoid repeating tasks; The two best I have yet found are Ajunta and MonoDevelop. Both are available from the repositories.
Now, if you want to "create some C and C++ programs to interact with the linux API from [your] windows XP", You need, for example, to learn sockets and SSH; so you can connect to your Linux machine from your Windows machine and execute some commands remotely from your Windows applications. But before you do that, learn C/C++ and play around with Linux. For a newbie, you already have your hands full right there.
Good luck!
[1] Ubuntu (a Linux distribution) comes with Gnome as GUI, while Kubuntu with KDE and Xubuntu has XFCE. All of them (GUIs) are separate projects and you could have all of them installed on any Linux desktop installation. Even, you don't need any GUI with any Linux distribution; for example, VMWare's Virtual Appliance Marketplate have a whole bunch of ready-to-go Linux installations like that.
Try it with a VM or as a live-cd.
Valgrind is a command-line tool but maybe some IDEs use integrated it.
Under Linux you'll see, than most of the time an IDE is quite useless (not a troll).
You'll do your Makefile manually,...
Hope you'll enjoy' it.
Regards,
Learn 1 thing at a time.
If you want to learn to program first, try python first. It works in Windows and Linux and you get result faster
If you want to learn C++, get Visual C++ express or Cygwin/GCC
If you want to experience with Linux, get a distribution of your choice (Linux-Mint is a good introction, coming from Windows) and try it in a VM (VMWare Player or VirtualBox)
Try easy projects and only after that, worry about debuggers
If you try it in virtual machines (virtualbox or WMWare for example) you can test as many options you want before deciding what Linux distro you will want to use. There are a lot, but from what you listed, my personnal opinion is that Ubuntu is a lot easier than Fedora to start. I've never used RedHat so I can't tell but it hasn't the reputation of beeing a hard one (for experts).
Anyways, at least to start I recommend installing it with GUI (and after starting too unless it's a server...).
Regarding IDEs, you could try Eclipse and Netbeans. They run both on Windows and Linux but I'm not C/C++ programmer so I don't know if they are good at that job. I you don't use IDE, Vim is far from beeing the unique option (Vim "addicts" :) will say it is the unique productive one but that's a personnal choice and the learning time is not very short). Personnaly I prefer a good IDE or at least graphical editors for programming, not that I don't like the power and speed of the terminal with command line as I prefer to use it for system administration or configuration but not for programming where you stay a long time on it.
I don't know about Valgrind but Eclipse or Netbeans IDEs have debuggers of course.
Programming for Linux a series of projects to learn, for the steps, you may refer to:
Red Hat Certified System Administrator I &II student-book which may help you to get the survival abilities in Linux, actually when you really understand the fields covered by these courses, you will have got the ability to find what to learn.
Search amazon with keyword 'Linux Programming', choose one and start your journey.
Have a good time.

Which install system to pick when deploying to Windows and Linux?

My company is thinking of dumping InstallShield and move to something else, mainly because of the poor experience it had with it, mostly on Linux.
Our product is a C++ application (binaries, shared libraries) targeted at Windows and Linux (Red Hat).
The installer itself isn't required to do anything special, just dump some binaries and shared libraries and sometime execute an external process. Things like version upgrading through the installer isn't necessary, this is handled after the installer finishes.
I thought of suggesting using NSIS on Windows and RPM on Linux.
What are the recommended installer systems to use when deploying to Windows/Linux? Something that is cross platform to prevent maintaining two installers is a definite plus.
For Windows I would definitively use NSIS. It's very lightweight, easy to code and very simple to understand. Using msis would just be a killer - it generates guid for every file so you can get upgrades for free and stuff but truth being said, you never end up using any of these.
Regarding Linux I would go for RPM and Deb. They're probably the two biggest packaging system so you'll be targeting most of the Linux users. I've never tried RPM but creating a Deb package is fairly straightforward.
See also:
What to use for creating a quick and light setup file?
Packaging to use to deploy cross-platform?
And even:
Creating installers for complex cross-platform programs
There's a tool called BitRock Installer which can create installers for Windows, Linux and OS X.
However, I think that if you target RedHat it would be better to provide native packages for that platform (that is .rpm).
For C++ projects, I'd go with cmake/cpack, if you are also willing to change your build system. Great support, strongly cross-platform. cpack has various generators, NSIS is one..
Take a look at InstallJammer. It will handle both platforms from the same build project, and you can have the installer register the package with the RPM database as well if that's your requirement.
You may want to consider our tool BitRock InstallBuilder , it can generate installers for Windows and Linux from a single project file and also RPMs. Is your application based on Qt? Our clients include the makers of Qt, Nokia (previously Trolltech) and they use it to package their Qt Creator product. We encourage to give InstallBuilder a try and contact our support with any questions or suggestions you may have.

Tools for Unix <-> Windows C++ development

I am doing some C++ cross development - been doing that for a while on Windows and recently started on Unix.
I suppose what I am after is to simplify Unix development experience - I have a local windows box I do development on, and a remote Solaris box which I use to compile and test code on unix environment.
What I do now - I develop, compiled and test code on Windows (VC++) and once it is done, I move code to Solaris box using Filezilla over SSH. I also use Putty to connect to Solaris box and execute shell commands.
Since I am quite new to unix development - I suppose what I do is by far not optimal and the tools/technics I use not optimal too.
Can you recommend me a better tools - how to move code around more easily and may be a replacement for Putty (which looks quite outdated anyway).
Thanks.
If by any chance you want to run the same C++ IDE on both Windows and Solaris, I recommend taking a look at Code::Blocks. Also, as I suggested to Charles, running an X server on the Windows box gives you a lot more flexibility than running Putty or similar.
Is there any reason that You can't test software on Solaris using Virtual Machine? They can share folders so there is no need to uploading code to remote machine.
Second: use svn or git or mercurial. In one machine You check in your code, on other you checkout plus You have history of changes. No need to use Filezilla over SSH.
edit:
Also, I think that it would be good to use cmake (or scons - but I don't used it) to generating build files. For example - cmake generates Makefiles or project files for Your IDE, so You don't need to maintaint few different files that build Your code on different platforms.
You might want to look into Samba, so you can work directly with the Windows file explorer to move files to and from Windows/Unix environments, rather than using FTP.
But for UNIX shell access via Windows, you really can't beat Putty.
I recommend mercurial.
Just use a version control system such as Subversion or Mercurial. I strongly recommend the latter because it's distributed so you don't need to have a server per say and you can work offline. Every time you want to shove your Windows code to the unix machine you just need to do 'hg push' and off you go. To sort out the build you can with good old Make or just use SCons (again I prefer the latter because it comes with the power of Python).
I actually, very recently developed a cross platform project in C++ using wxWidgets and GraphicsMagick. I wrote it all in Mac OS X and then compiled both in Windows and Linux. One thing I'd like to point out is that GCC seems to be more pedantic about compile warnings and errors than Microsoft's compiler so if you grow to like the Unix environment I'd recommend to develop there and then compile in Windows (maybe even using a VMWare image).
Instead of moving your source code around manually, consider using a version control system. Not necessarily a distributed VCS such as git or mercurial, but you should use version control nonetheless.
Sooner or later, you'll need to use a debugger on the Unix machine, and if you prefer using a graphic debugger, you should install a local X server on your Windows machine.
IMHO vim is quite good editor ;)
gcc, nm, ld for compilation/build/diagnosis
makefile for builds
gdb as debugger, if you prefer GUI check ddd (if you want to stick
with Visual Studio for debugging
check www.vsbridge.com or
www.wingdb.com - they both
depends on gdb as back-end)
other commercial debugger for Unixes
is TotalView
(http://www.roguewave.com/products/totalview.aspx the price is
high, although they have their own
engine instead of gdb)
CVS, SVN as source control
If you want to edit files in VisualStudio you can use e.g. Samba as "transparent file system" ;)
By the way VirtualBox may be very helpful (I debug (Open)Solaris or Linux as VBox machines very frequently).
ps
yet another environment you may be interested in is Magic C++ www.magicunix.com/

Reading from the serial port from C++ or Python on windows

I need to read the serial port from windows, using either Python or C++. What API/Library should I use? Can you direct me to a tutorial? Thanks!
In python you've excellent package pyserial that should be cross-platform (I've used only in GNU/Linux environment).
Give it a look, it's very simple to use but very powerful!
Of course examples are provided!
By the way, if it can be useful here you can find a project of mine which use pyserial, as an extended example.
In C++:
CreateFile("\\\\.\\COM39", ...)
SetCommState
SetCommTimeouts
ReadFile, WriteFile
CloseHandle
There is also a full documentation on communication resources.
This is classic article about Win32 serial communications:
http://msdn.microsoft.com/en-us/library/ms810467.aspx
In python it is as simple as importing pyserial and use its open() method (if you are using quite standard system, otherwise you have to adjust a number of parameters to match your environment of course).
EDIT:
As pointed out by Enrico, pyserial is not a default package in many distributions, so you have to install it by your own.
I suggest to install and use easy-install, by setuptools to handle your python addons packages.