Is POCO multiprocessing library a wrapper? - multiprocessing

Is the POCO multiprocessing library a wrapper around POSIX fork() and Windows' CreateProcess?
Considering I only have to use multiprocessing, is it worth using POCO? Does it have a modular structure that allows to pick bits and pieces individually?
Is it better to just use the platform specific libraries with conditional compilation?
Is there any other choice?

It is true that Poco process class is carefully designed wrapper class for platform specific api.
You can checkout its code.
Process.h
Process_UNIX.h
Process_WIN32.h
Process_WINCE.h
#if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
#if defined(_WIN32_WCE)
#include "Process_WINCE.h"
#else
#include "Poco/Process_WIN32U.h"
#endif
#elif defined(POCO_OS_FAMILY_WINDOWS)
#include "Poco/Process_WIN32.h"
#elif defined(POCO_VXWORKS)
#include "Poco/Process_VX.h"
#elif defined(POCO_OS_FAMILY_UNIX)
#include "Poco/Process_UNIX.h"
#else
#include "Poco/Process_VMS.h"
#endif
You only need to link its Foundation library to your executable, so it has modular architecture.

Related

What is __XSI_VISIBLE?

I was working on a C/C++ project for an embedded system that uses gcc-arm-none-eabi-8-2019-q3-update as a compiler.
I added the use of the strptime function of time.h but initially it was undefined and I found in the compiler inclusions:
#if __XSI_VISIBLE
...strptime...
#endif
So, I solved the problem with:
#undef __XSI_VISIBLE
#define __XSI_VISIBLE 1
#include <time.h>
#undef __XSI_VISIBLE
#define __XSI_VISIBLE 0
Now it works BUT:
What have I done?
What is __XSI_VISIBLE?
What is it for?
Why does this compiler keep it by default at 0?
From https://pubs.opengroup.org/onlinepubs/9699919799/:
The X/Open System Interfaces (XSI) option is the core application programming interface for C and sh programming for systems conforming to the Single UNIX Specification. This is a superset of the mandatory requirements for conformance to POSIX.1-2017.
The __XSI_VISIBLE macro makes visible extensions to "vanilla" POSIX interfaces, which otherwise would be forbidden to be in the name space. Remember that C language standards like ISO C and POSIX permit the application to define all non-standard identifiers (in ISO C and "vanilla" POSIX, strptime is not reserved, you can write a function with that name and have it not interfere). By defining so-called feature test macros you extend the set of standard identifiers and reduce those available to define by the application programmer.
Your compiler keeps it at 0 because the implementation vendor chose that it is the application programmer's job to enable XSI when s/he wants it. Application programmers do this by defining the desired feature test macros before header inclusion, e.g. with
#define _POSIX_SOURCE
#define __XSI_VISIBLE 1
#include <time.h>
or pass -D__XSI_VISIBLE=1 to the compiler.
The correct defs to use are -D_XOPEN_SOURCE=1 and -D_GNU_SOURCE=1. These are used to conditionally define __XSI_VISIBLE and __GNU_VISIBLE respectively in <sys/features.h>. Defining __XSI_VISIBLE and __GNU_VISIBLE will not always work because they are overridden in <sys/features.h>.

How do you use opengl functions?

I'm trying to build the source code provided for the book Mathematics for 3D Game Programming and Computer Graphics. I've linked the OpenGL.Framework in "Build Phases" and included (not sure which one I need)
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
Now I get
Use of undeclared identifier 'Sqrt'
Use of undeclared identifier 'InverseSqrt'
Use of undeclared identifier 'fabs'
I'm guessing these have to do with not setting up OpenGL properly?
The author mentions using GLSL in the book but doesn't go into the details. I'm new to OpenGL.
not a xcode coder but in C++ fabs,sqrt are in math.h and if InverseSqrt means sqr so you can try to do a fix like this:
#include <math.h>
#define Sqrt sqrt
#define InverseSqrt(x) (x*x)
some environments want this instead:
#include <math>
#define Sqrt sqrt
#define InverseSqrt(x) (x*x)
However as mentioned in the comment those functions have nothing to do with OpenGL so they are most likely used in some lib you have included/linked whatever ... and forgot to include some header you should ...
[Edit1]
If InverseSqrt means 1/sqrt(x) as derhass suggested (english terminology feels sometimes weird) then use
#define InverseSqrt(x) (1/sqrt(x))
inversesqrt (no caps) is a built in function in GLSL, while fabs is a function in C, and sqrt exists in both languages. Xcode can compile C/C++, but you must write code to compile GLSL.

Error when including winuser.h. It defines ChangeMenu to ChangeMenuW or ChangeMenuA

Working on a Qt app on Windows. I include QVboxLayout in my source file only and this causes errors because its macro overwrites my method name.
foo.hpp
class foo
{
ChangeMenu();
}
foo.cpp
#include "foo.hpp"
#include "QVBoxLayout" // <--- this includes winuser.h
foo::ChangeMenu(){};
Now what happens is winuser.h has a macro
#ifdef UNICODE
#define ChangeMenu ChangeMenuW
#else
#define ChangeMenu ChangeMenuA
#endif // !UNICODE
This changes my function definition to ChangeMenuW but my declaration is still ChangeMenu.
How should I solve this? How can winuser.h define such a "normal" name as a macro?
Version of winuser.h is "windows kits\10\include\10.0.16299.0"
Pretty much any Windows API that deals with strings is actually a macro that resolves to a A or W version. There's no way around, you can either:
avoid including windows.h, but as you noticed, it creeps through;
brutally #undef the macro before defining/using your function; this is a fit punishment for hoarding such normal and non-macro-looking identifiers, but is tedious and some other code may actually need the Win32 function;
just accept it as a sad fact of life and avoid all the relevant Win32 APIs names; if you use Qt and follow its naming convention, it should be easy, as Qt functions use lowerCamelCase (as opposed to Win32 UpperCamelCase);
include windows.h explicitly straight in your header (possibly under an #ifdef _WIN32); this will make sure that your identifier will get replaced by the macro in all instances, so everything will work fine even if the compiler will actually compile a function with a different name; suitable for standalone projects, not suitable for libraries. (Thanks #Jonathan Potter for suggesting this)
You could take no care about this issue, Although your method name will be the same as the windows API, but the system will not mix them(just unify Unicode on both the place to define/call). If you call the ChangeMenu() directly, you will call the winapi, and if
foo f;
f.ChangeMenu();
or
foo::ChangeMenu();(static)
You will call your method.
And if you want to disable the winapi:
#ifdef ChangeMenu
#undef ChangeMenu
//code place that you define/call your own ChangeMenu().
#ifdef UNICODE
#define ChangeMenu ChangeMenuW
#else
#define ChangeMenu ChangeMenuA
#endif // !UNICODE
#endif
(It looks very tedious.)

What does #defining WIN32_LEAN_AND_MEAN exclude exactly?

I found the explanation defining WIN32_LEAN_AND_MEAN "reduces the size of the Win32 header files by excluding some of the less frequently used APIs". Somewhere else I read that it speeds up the build process.
So what does WIN32_LEAN_AND_MEAN exclude exactly? Should I care about this pre-processor directive? Does it speed up the build process?
I've also seen a pre-processor directive in projects named something along the lines of extra lean. Is this another esoteric pre-processor incantation I should know about?
According the to Windows Dev Center WIN32_LEAN_AND_MEAN excludes APIs such as Cryptography, DDE, RPC, Shell, and Windows Sockets.
Directly from the Windows.h header file:
#ifndef WIN32_LEAN_AND_MEAN
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
#include <dlgs.h>
#ifndef _MAC
#include <lzexpand.h>
#include <mmsystem.h>
#include <nb30.h>
#include <rpc.h>
#endif
#include <shellapi.h>
#ifndef _MAC
#include <winperf.h>
#include <winsock.h>
#endif
#ifndef NOCRYPT
#include <wincrypt.h>
#include <winefs.h>
#include <winscard.h>
#endif
#ifndef NOGDI
#ifndef _MAC
#include <winspool.h>
#ifdef INC_OLE1
#include <ole.h>
#else
#include <ole2.h>
#endif /* !INC_OLE1 */
#endif /* !MAC */
#include <commdlg.h>
#endif /* !NOGDI */
#endif /* WIN32_LEAN_AND_MEAN */
If you want to know what each of the headers actually do, typing the header names into the search in the MSDN library will usually produce a list of the functions in that header file.
Also, from Microsoft's support page:
To speed the build process, Visual C++ and the Windows Headers provide
the following new defines:
VC_EXTRALEAN
WIN32_LEAN_AND_MEAN
You can use them to reduce the size of the Win32 header files.
Finally, if you choose to use either of these preprocessor defines, and something you need is missing, you can just include that specific header file yourself. Typing the name of the function you're after into MSDN will usually produce an entry which will tell you which header to include if you want to use it, at the bottom of the page.
Complementing the above answers and also "Parroting" from the Windows Dev Center documentation,
The Winsock2.h header file internally includes core elements from the Windows.h header file, so there is not usually an #include line for the Windows.h header file in Winsock applications. If an #include line is needed for the Windows.h header file, this should be preceded with the #define WIN32_LEAN_AND_MEAN macro. For historical reasons, the Windows.h header defaults to including the Winsock.h header file for Windows Sockets 1.1. The declarations in the Winsock.h header file will conflict with the declarations in the Winsock2.h header file required by Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header ..
Here's a good answer on the motivation for it from Raymond Chen's blog:
https://devblogs.microsoft.com/oldnewthing/20091130-00/?p=15863
...defining WIN32_LEAN_AND_MEAN brought you back to the 16-bit Windows philosophy of a minimal set of header files for writing a bare-bones Windows program. This appeased the programmers who liked to micro-manage their header files, and it was a big help because, at the time the symbol was introduced, precompiled header files were not in common use. As I recall, on a 50MHz 80486 with 8MB of memory, switching to WIN32_LEAN_AND_MEAN shaved three seconds off the compile time of each C file. When your project consists of 20 C files, that’s a whole minute saved right there.

How to check if the current operating system is Windows, Linux, or OSX?

I'm writing a compiler project which will produce assembly code as the target language. However, there are some small changes that need to be accounted for depending on the operating system, and I'm not sure how to check for the OS. In case it matters, I am concerned with only 32bit. I've seen in some source code something like
#ifdef WIN32
but I have no idea how/if this works.
EDIT: Some clarification. I am using gcc on all three platforms. I don't know if macros like WIN32 are defined via gcc in each platform. If so, these constants seem to solve my issue.
The use of #ifdef uses preprocessor symbols to conditionally compile code depending on which symbols are defined. In your example, the compiler or an #include file may define WIN32, which means the code is being compiled in a Win32 environment.
Depending on the compiler and the platform, there may be different predefined symbols that indicate the operating system or processor architecture (among many other possible things) that relate to the current compilation environment.
With gcc, you can show the predefined symbols using the following command line:
gcc -E -dM - </dev/null
On my machine, one of the defined symbols is:
#define __FreeBSD__ 8
This happens to mean that I'm running FreeBSD version 8.
(The above applies to C and C-family languages such as C++.)
Generally the essential platform-determinign predefined macros are defined by the compiler itself, often depending on other switches.
Best way for a given compiler is to search for "predefined macros" or "predefined symbols" in their help. eg: googling visual studio predefined macros yields this page.
My OOFILE frameworks covered a wide range of systems. The oofplat.h is available from Sourceforge svn browser and unifies the different compiler definitions into _Windows etc.
Relevant portions copied below. Note that it's fairly ignorant when it comes to differentiating Unix versions as there weren't built-ins defined by the compiler.
#ifndef _Windows
#if defined(_WIN32)
#define _Win32
#define _Windows
#elif defined(WIN32)
#define _Win32
#define _Windows
#elif defined(__WIN32__)
#define _Win32
#define _Windows
#elif defined(__Win32__)
#define _Win32
#define _Windows
#elif defined(_WINDOWS)
#define _Windows
#elif defined(__INTEL__) && defined(__MWERKS__)
// Metrowerks CodeWarrior doesn't build anything other than Win32 on INTEL, no DOS
#define _Windows
#define _Win32
#endif
#else
#if defined __Win32__ || defined _WIN32
#ifndef _Win32
#define _Win32
#endif
#endif
#endif
#ifndef _MSDOS
#ifdef _Windows
#define _MSDOS
#elif defined(MSDOS)
#define _MSDOS
#elif defined(__MSDOS__)
#define _MSDOS
#endif
#endif
#ifdef _Windows
#ifndef STRICT
// some Windows headers define STRICT. In Visual C++ at least having it defined
// affects how static member signatures are mangled, so we define it up front
#define STRICT
#endif
#endif
// if not a DOS machine by now, may be Mac or Unix
// cope with Metrowerks and Symantec (and MPW?)
#ifndef _MSDOS
#ifndef _Macintosh
#ifdef macintosh
#define _Macintosh
#endif
#endif
#ifndef _Macintosh
#define _Unix
#endif
#endif
This is the power of the C preprocessors. First at all, you can define macros:
#define MY_MACRO
After this, you can check if a macro is defined:
#ifdef MY_MACRO
code, code, code
code, code, code
#endif
That means the code inside #ifdef and #endif blocks will be valid only if that macro was defined. Else it'll be ignored, and with ignored I mean it'll be like you haven't never written it.
The opposite of #ifdef is #ifndef. #endif is the same for both cases.
In your case, you can use macros in order to change what a function will do:
#define MY_MACRO
void my_function() {
#ifdef MY_MACRO
code_for_my_macro();
#endif
#ifdef ANOTHER_MACRO
code_for_another_macro();
#endif
}
..so the only thing you'll need to do to port your code is changing the macro.
Macros have more utilities. Search for "C preprocessors" and you'll see all what you can do with them.

Resources