__POSIX_VISIBLE is ruining my pthreading on cygwin
Asked Answered
A

2

8

I'm compiling a project that uses pthreads with gcc (g++ exactly) on cygwin. Seeing as how I have always developed this on linux, I never had any problems.

But right now, I am unable to compile code that uses some pthread functions with the compiler complaining that these functions are not declared. I realized that the pthread.h that came with my toolchain, g++ (GCC) 5.4.0, has guarded sections with

#if __POSIX_VISIBLE >= 200112

and these are preventing functions from being available on cygwin/windows.

I attempted searching "__POSIX_VISIBLE" but did not find anything really helpful. What does this actually do and why? I assume it's just that some parts of pthread can't work on windows, or within cygwin or whatever. But why would only some of pthread be blocked under this anyway? It doesn't even complain about -pthread being used.

I plan on changing all the pthread stuff to c++ threads eventually, but right now I'd just like this to compile so I can work on more immediate problems. Is there any way around this if I'm doing it on cygwin/windows?

Anachronistic answered 1/7, 2016 at 7:35 Comment(3)
Have you told the compiler you want POSIX features? Or GNU features? Try -std=gnu++14 or -D_XOPEN_SOURCE=700 and see if that sorts things out.Plywood
-std=gnu++14 worked, thanks. I didn't know about that. That's a bit strange to me though. Why wouldn't that be on by default? The reason I'm using cygwin in the first place is because I want POSIX and GNU.Anachronistic
There could be lots of factors. A primary one would be which version of GCC you're using — older versions won't use the most recent standard by default. (IIRC, GCC 5 switched to C++11 by default; version GCC 6 uses C++14 by default.)Plywood
G
5

looks on /usr/include/sys/features.h for details.

Default is _GNU_SOURCE that includes everthing. Check if your project is restricting the definition with -std or ansi

Goatsucker answered 1/7, 2016 at 8:32 Comment(2)
It took way too long to find out this was the problem. It turns out, -std=c++NN disables some POSIX features apparently. Replacing it with -std=gnu++NN seems to fix everything.Skyjack
std=c++NN enables only the features present in the C standard, all the rest is disabledGoatsucker
F
-1

[SOLUTION FOUND]

Hello (I'm posting this on 2 threads regarding the same problem).

I'm here because I had the "POSIX_VISIBLE >= 200112" and the "posix_memalign was not declared in this scope" issue, which was halting the compilation of a program.

I'm not a programmer and tried various fixes on my own for a couple hours. Then finally Googled & came upon this site. The solutions here did not work for me, but I'll describe what did work:

The "posix_memalign" text was in a "stdlib.h" file that was being included into the code. The first problem was that in my "cygwin" directory, I have 25 instances of "stdlib.h"! Which one of those was being included?! I'm all new to this, but I finally found that

echo | gcc -E -Wp,-v -

might at least give an idea of which directory the files were being "included" from. This narrowed down the number of "stdlib.h" files to 4. Out of 4 such files, only one had the "posix_memalign" text. I tried changing the filename of that stdlib.h to see if it would cause an error--and confirm that it was the stdlib.h in question. However, this didn't effect the program. So I searched for a "stdlib.h" file in the next directory higher. THAT "stdlib.h" file also had the "POSIX" text in it. So when I changed THAT stdlib.h filename, the program DID error out. So that was the stdlib.h to deal with.

I saw that the "POSIX_VISIBLE >= 200112" instruction effected only the ONE line of code with "posix_memalign" in it. (In other words, the "POSIX_VISIBLE" instruction was not being applied to the whole file.) I considered "commenting" it out, or deleting it. But then non-programmer me got the ingenious idea to simply change the ">=" to a "<". So I now had "POSIX_VISIBLE < 200112". I saved the file, ran the "configure" and "make" routine again, and boom, all was well. Program compiled properly.

Moral of the story, if you can determine the file (containing the POSIX statement and the posix_memalign) which is being accessed by your code, you may be able to solve your problem by just changing that one POSIX_VISIBLE operator as I did. (And you may want to switch that operator back after your compiling is done, in case that stdlib.h library file needs to be used by other programs in the future.)

Feriga answered 14/1, 2022 at 18:57 Comment(1)
I am glad you solved your problem, and it sounds like you followed a thoughtful and logical troubleshooting process. However, for the sake of others who may come across this: modifying system header files is absolutely the wrong way to solve any C compilation problem. If you are interested in what you could have or should have done instead then that might be a good topic for a question of its own.Reddish

© 2022 - 2024 — McMap. All rights reserved.