MinGW error about TITLEBARINFO not declared - c++

I'm using the latest MinGW:
MINGWBASEDIR=C:\mingw
gcc version 4.7.0 (GCC)
gcc version 4.7.0 (GCC)
GNU gdb (GDB) 7.5
GNU ld (GNU Binutils) 2.22
GNU windres (GNU Binutils) 2.22
GNU dlltool (GNU Binutils) 2.22
GNU Make 3.82.90
#define __MINGW32_VERSION 3.20
#define __W32API_VERSION 3.17
This is the source file (test.cpp):
#include <stdio.h>
#include <windows.h>
int main(int Argc,char** Args) {
printf("%d",sizeof(TITLEBARINFO));
}
This is how I compile it:
g++ -c test.cpp
And this is the error message reported by MinGW:
C:\temp>g++ -c test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:5:22: error: 'TITLEBARINFO' was not declared in this scope
TITLEBARINFO is supposed to be declared in winuser.h which windows.h includes.
I've checked these 2 header files in mingw/include dir, the definition of TITLEBARINFO is truly there. But I still get this compiler error, how strange!

gcc -c -D_WIN32_WINDOWS=0x0410 test.cpp
MinGW apparently defaults to 0x0400.

Related

How to resolve `clang: error: unsupported option '-fsanitize=leak'` on apple devices

I am trying to run a simple leak check program.
#include <iostream>
int main()
{
double *ptr = new double(3.14);
}
using the command
g++ -g -fsanitize=leak -o main main.cpp
and I get the following error:
clang: error: unsupported option '-fsanitize=leak' for target 'x86_64-apple-darwin20.1.0'
I stopped using the clang that comes with Xcode and installed clang/LLVM using homebrew.
$ which clang++
/usr/local/opt/llvm/bin/clang++
clang++ --version
clang version 11.0.0
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
EDIT: When I was using apple clang, g++ used to default to clang++. Apparently that changed when I installed llvm/clang. Thanks to #cigien for pointing it out. g++ still uses default to the compiler that that comes with Apple clang.
g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
According to this answer you should use:
g++-10 -g -fsanitize=leak -o main main.cpp (in this cases (with flag leak) there is no leak message for me ./main, but it compile) then it should be:
g++-10 -fsanitize=address -g main.cpp ; ASAN_OPTIONS=detect_leaks=1 ./a.out and it detect leak well.
Note, that the correct path of brew installed g++ in MacOS is:
$ which g++-10
> /usr/local/bin/g++-10
--
$ which g++
> /usr/bin/g++ //this is pseudonym of clang
The same for gcc-10 (10 is my current version. You should use your version instead of that)
If you use CMakeLists.txt file you will configure it like this:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")
# leak sanitizer_name not works for me. should be address
And should execute cmake command like this:
cmake -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 ..
And then ASAN_OPTIONS=detect_leaks=1 ./a.out
Note, that if you open */CMakeFiles/3.18.4/CMakeCXXCompiler.cmake file you will observe the compiled info, and now it will be g++.

Compile with c++17 mac

I can't compile with -std=c++17, I got :
error: invalid value 'c++17' in '-std=c++17'
However I update Xcode and clang.
My Clang version is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
And I load the newest header like optional, I have to do
#include <experimental/optional>
instead of
#include <optional>
Xcode brings its own complete toolchain, including headers and the actual compiler.
Apple LLVM version 9.0.0 (clang-900.0.39.2) (which ships with Xcode 9.2) does not support the usage of the flag -std=c++17 since its too old. The optional header is only included under the folder experimental/. Which is why you need to #include <experimental/optional>
In order to compile your program with c++17 support using the compiler which comes with Xcode 9.2 you need to use the -std=c++1z flag.
Xcode 9.3 will be shipped with Apple LLVM version 9.1.0 (clang-902.0.30) which has support for the -std=c++17 flag. However the optional header is as of today still under the experimental/ subdirectory. This might change during the betas.
Here is what I get with this tests:
#include <experimental/optional>
int main(int, char* []) {
return 0;
}
g++ -std=c++17 -o test test.cpp
error: invalid value 'c++17' in '-std=c++17'
g++ -std=c++1z -o test test.cpp
Did you try the c++1z argument?
Also of note my test compiles without the -std=c++1z argument provided.
I think I'm on a newer version of OSX than you:
Target: x86_64-apple-darwin17.4.0
You should use -std=c++1z as flag.
libc++ with c++17 support since macos 15
-std=c++1z also works on Apple LLVM version 8.1.0 (clang-802.0.42)

Can I use CLang and libstdc++ (emmintrin.h)

The following code
#include <iostream>
int main(void)
{
std::cout << "Hello World!" << std::endl;
return 0;
}
compiles fine with both g++ and clang
g++ test.cpp
clang -I /usr/lib/gcc/x86_64-linux-gnu/4.7/include -I /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed -lstdc++ test.cpp
where /usr/lib/gcc/x86_64-linux-gnu/4.7/ points to the include files of the libstdc++
However, when I add an additional #include <emmintrin.h>
the clang compilations fails with
In file included from test.cpp:2:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/emmintrin.h:36:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.7/include/xmmintrin.h:36:
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/mmintrin.h:46:72: warning: unknown attribute '__artificial__' ignored [-Wunknown-attributes]
extern __inline void __attribute__((__gnu_inline__, __always_inline__, __artificial__))
^
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/mmintrin.h:52:72: warning: unknown attribute '__artificial__' ignored [-Wunknown-attributes]
extern __inline void __attribute__((__gnu_inline__, __always_inline__, __artificial__))
...
/usr/lib/gcc/x86_64-linux-gnu/4.7/include/xmmintrin.h:120:19: error: use of undeclared identifier '__builtin_ia32_divss'; did you mean '__builtin_ia32_minss'?
return (__m128) __builtin_ia32_divss ((__v4sf)__A, (__v4sf)__B);
...
According to http://en.cppreference.com/w/cpp/header, the `xmmintrin.h' is not part of the interface of the standard C++ library and hence I guess it contains Gnu specific stuff. Can I somehow compile this code using clang?
If not, how can I easily install the clang libc++?
sudo aptitude install libc++-dev doesn't find a package (while this does work on Ubuntu 16.04)
I'm using
"Debian GNU/Linux 7 (wheezy)"
gcc version 4.7.2 (Debian 4.7.2-5) // Target: x86_64-linux-gnu
clang version 3.6.0 (trunk 224504) // Target: x86_64-unknown-linux-gnu
Apparently, my clang installation was incomplete.
After downloading llvm and clang (v3.7.1 since v4.0.1 required gcc4.8 while I only have 4.7) and building it locally clang -lstdc++ test.cpp works fine in both cases.

C++ Compile error on NetBSD '::system' has not been declared

I have some C++ code that compiles nicely on Linux but so far I'm having trouble getting it to compile correctly on NetBSD.
These are my includes:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
#include <boost/regex.hpp>
I compile with this command:
g++ -v -O2 -fno-strict-aliasing -I /usr/pkg/include \
barefoot.cpp -o barefoot -L /usr/pkg/lib/ -lboost_regex \
-L /usr/pkg/lib/ -lboost_regex -lpthreads
Which renders this output:
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64--netbsd
Configured with: /usr/src2/tools/gcc/../../external/gpl3/gcc/dist/configure --target=x86_64--netbsd --enable-long-long --enable-threads --with-bugurl=http://www.NetBSD.org/Misc/send-pr.html --with-pkgversion='NetBSD nb2 20111202' --enable-__cxa_atexit --with-tune=nocona --with-mpc=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-mpfr=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-gmp=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --enable-tls --disable-multilib --disable-symvers --disable-libstdcxx-pch --build=x86_64-unknown-netbsd5.99.56 --host=x86_64--netbsd
Thread model: posix
gcc version 4.5.3 (NetBSD nb2 20110806)
COLLECT_GCC_OPTIONS='-v' '-O2' '-fno-strict-aliasing' '-I' '/usr/pkg/include/' '-o' 'a.out' '-L' '/usr/pkg/lib/' '-mtune=nocona' '-march=x86-64'
/usr/libexec/cc1plus -quiet -v -I /usr/pkg/include/ barefoot.cpp -quiet -dumpbase barefoot.cpp -mtune=nocona -march=x86-64 -auxbase barefoot -O2 -version -fno-strict-aliasing -o /var/tmp//cc9Dcmwi.s
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
#include "..." search starts here:
#include <...> search starts here:
/usr/pkg/include/
/usr/include/g++
/usr/include/g++/backward
/usr/include/gcc-4.5
/usr/include
End of search list.
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
Compiler executable checksum: a34e7d170f4dd8d4687d2b62e8dca4b7
In file included from /usr/include/g++/bits/gthr.h:166:0,
from /usr/include/g++/ext/atomicity.h:34,
from /usr/include/g++/bits/ios_base.h:41,
from /usr/include/g++/ios:43,
from /usr/include/g++/istream:40,
from /usr/include/g++/fstream:40,
from barefoot.cpp:29:
/usr/include/g++/bits/gthr-default.h:130:9: error: 'pthread_detach' was not declared in this scope
/usr/include/g++/bits/gthr-default.h:130:1: error: invalid type in declaration before ';' token
/usr/include/g++/bits/gthr-default.h: In function 'int __gthread_detach(pthread_st*)':
/usr/include/g++/bits/gthr-default.h:668:46: error: '__gthrw_pthread_detach' cannot be used as a function
In file included from /usr/pkg/include/boost/regex/v4/regex_workaround.hpp:25:0,
from /usr/pkg/include/boost/regex/v4/regex.hpp:32,
from /usr/pkg/include/boost/regex.hpp:31,
from barefoot.cpp:32:
/usr/include/g++/cstdlib: At global scope:
/usr/include/g++/cstdlib:132:11: error: '::system' has not been declared
I tried adding -lpthread, but that resulted in the same output. Not sure what to try next.
I do not know exactly why you are facing this problem, but I can provide you with some next steps to try.
Notice that the first error is that pthread_detach is missing. The system header files will typically make sure whatever headers they need are included, but you can try adding <pthread.h> above all the other headers.
If that fails, you need to figure out which file got included as a result of adding #include <pthread.h>, and make sure pthread_detach is present inside it (it really should be there somewhere).
Assuming it is there, there must be some conditional compilation that is causing it to not be visible to your source code. Find the conditional guards, and what macro values affect it.
Then, you will need to see why your system is defining the macro values in a way that causes the function pthread_detach to not be visible.
With this investigation, you may discover that you are not including the right <pthread.h> file. This may be due to system include directories conflicting with the include directories you want to use.
This answer (it is towards the bottom) to this question shows how to display the predefined macros for the g++ compiler.

Getting GCC in C++11 mode to work on FreeBSD

How do I get a working GCC-based C++11 setup on FreeBSD 10? It seems that the standard library that comes with recent GCC versions on FreeBSD is broken. I've installed the port gcc49 and then try to compile this:
#include <string>
int main()
{
auto str = std::to_string(42);
str = std::to_string(42ull);
str = std::to_string(4.2);
str.clear();
return 0;
}
This gives me an error:
g++49 -v -std=c++11 foo.cc
Using built-in specs.
COLLECT_GCC=g++49
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/lto-wrapper
Target: x86_64-portbld-freebsd10.0
Configured with: ./../gcc-4.9-20141022/configure --disable-nls --enable-gnu-indirect-function --libdir=/usr/local/lib/gcc49 --libexecdir=/usr/local/libexec/gcc49 --program-suffix=49 --with-as=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/gc
c49/include/c++/ --with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --with-ecj-jar=/usr/local/share/java/ecj-4.5.jar --enable-languages=c,c++,objc,fortran,java --prefix=/usr/local --mandir=/usr/local/man --infodir=/usr/local/info/gcc49 --build=x86_64-portbld-freebsd10.0
Thread model: posix
gcc version 4.9.2 20141022 (prerelease) (FreeBSD Ports Collection)
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/cc1plus -quiet -v foo.cc -quiet -dumpbase foo.cc -mtune=generic -march=x86-64 -auxbase foo -std=c++11 -version -o /tmp//ccbNFhtI.s
GNU C++ (FreeBSD Ports Collection) version 4.9.2 20141022 (prerelease) (x86_64-portbld-freebsd10.0)
compiled by GNU C version 4.9.2 20141022 (prerelease), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/../../../../../x86_64-portbld-freebsd10.0/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc49/include/c++/
/usr/local/lib/gcc49/include/c++//x86_64-portbld-freebsd10.0
/usr/local/lib/gcc49/include/c++//backward
/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/include
/usr/local/include
/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/include-fixed
/usr/include
End of search list.
GNU C++ (FreeBSD Ports Collection) version 4.9.2 20141022 (prerelease) (x86_64-portbld-freebsd10.0)
compiled by GNU C version 4.9.2 20141022 (prerelease), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 8405316ee381c37148f55a17e42da47a
foo.cc: In function 'int main()':
foo.cc:5:14: error: 'to_string' is not a member of 'std'
auto str = std::to_string(42);
^
foo.cc:6:9: error: 'to_string' is not a member of 'std'
str = std::to_string(42ull);
^
foo.cc:7:9: error: 'to_string' is not a member of 'std'
str = std::to_string(4.2);
^
The default include search path is /usr/local/lib/gcc49/include/c++/, which does contain the string header. Makes sense, because the gcc49 port ships with a corresponding libstc++. But it seems broken, per the above error.
Is this a known problem? Has anyone gotten a working setup of a GCC-based toolchain for C++11 on FreeBSD?
(Note: I'm aware that Clang is the default compiler on FreeBSD 10. But in this case I'm specifically looking to support a GCC-based toolchain.)
Turns out that adding the following flags to g++ will make it work:
-D_GLIBCXX_USE_C99 -D_GLIBCXX_USE_C99_MATH -D_GLIBCXX_USE_C99_MATH_TR1