GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope - c++

GoogleTest 1.6 with Cygwin 1.7: 'fileno' was not declared in this scope
Error message when building a simple test on Factorial() function in Eclipse CDT:
Invoking: Cygwin C++ Compiler
g++ -std=c++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
In file included from E:\source\gtest-1.6.0\include/gtest/internal/gtest-internal.h:40:0,
from E:\source\gtest-1.6.0\include/gtest/gtest.h:57,
from ../src/challenge.cpp:11:
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1589:51: error: 'fileno' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1595:57: error: 'strdup' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1627:71: error: 'fdopen' was not declared in this scope
Eclipse CDT 8.1 running gcc 4.7.3 on Cygwin 1.7.22
gTest 1.6 succesfully built including demo tests, with cmake 2.8.9 on Cygwin 1.7.22
I've linked the built lib with full path, E:\lib\gtest-1.6.0\Cygwin\libgtest.a
The following command option was added manually, got same error without it.
-DGTEST_OS_CYGWIN=1
Seems the errors have nothing to do with my code. Anyone using gTest with Eclipse and Cygwin?
Thank you,
unsigned long Factorial(unsigned n) {
return n==0? 0 : n*Factorial(n-1);
}
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}

Setting the C++ standard to -std=gnu++0x rather than -std=c++0x, worked for me. You can try the statement:
g++ -std=gnu++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
Setting symbol (-DGTEST_OS_CYGWIN=1) has got nothing to do with this error.

Some functions go beyond the ANSI standard.
These are disabled when you use std=c++11 (or std=c++0x).
Among them are fdopen, fileno and strdup.
There are two possibilities to use them:
Use the GNU dialect (std=gnu++11).
If you want to compile without dialect and make a local exception, you can include stdio.h with the __STRICT_ANSI__ undefined. (see: Error "'fdopen' was not declared" found with g++ 4 that compiled with g++3)
I have tested both on Suse Linux Enterprise 11, MinGW and Cygwin.
Addition: Another (possibly better) way to access non-ANSI symbols would be to add
#define _POSIX_C_SOURCE 200809L
before the first #include in your file. This will give you access to most of the non-standard routines.
Some functions (e.g. realpath(...)) require
#define _BSD_SOURCE
to be inserted on top of your file.

Related

Need help compiling with g++ if the code have threading functionalities using std::thread [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

Issue after GCC upgrade C++

Current Code Base
Os-Linux x64, have perl code, boost 1.42, GCC 4.4.7 and other dependencies as a part of project code.
The build works fine.
After Upgrade of GCC to 4.9.2
We made a copy of existing Linux ec2 instance (which works fine) and upgraded its GCC to 4.9.2.
When we are trying to build the project, getting the below error.
webql/rt/bootstrap: Compiling main.cxx
webql/rt/bootstrap: #/archive/webql/build/al/current/base/dev/general/gfilt -pthread -fPIC -DU_HAVE_NAMESPACE=1 -D_FILE_OFFSET_BITS=64 -D_unix -D_BSD -fexceptions -fmessage-length=0 -I/home/builder/archive/webql/build/al/current/base/dev/perl/core -DQL2_PLATFORM="al" -DJAVA_ARCH="amd64" -fvisibility=hidden -D_XOPEN_SOURCE=500 -I/archive/webql/build/al/current/base/dev/general -I/archive/webql/build/al/current/base/dev/general/include -I. -I/archive/webql/build/al/current/base/dev -I/archive/webql/build/al/current/base/dev/xml -I/archive/webql/build/al/current/base/dev/xml/libxml -I/archive/webql/build/al/current/base/dev/xml/libxslt -I/archive/webql/build/al/current/base/packages/al/inst/include -I/archive/webql/build/al/current/base/dev/zlib -I/archive/webql/build/al/current/base/packages/al/inst/jdk/include -I/archive/webql/build/al/current/base/packages/al/inst/jdk/include/linux -I/usr/java/include -I/usr/java/include/linux -I/mnt/usr/include -ggdb3 -O3 -DNDEBUG -c -o /archive/webql/build/al/current/base/dist/alr/obj/webql/rt/bootstrap/main.o main.cxx
In file included from ../perlapi/perl.h::,
from ../perlapi/Utils.h:,
from main.cxx::
/archive/webql/build/al/current/base/dev/perl/core/perl.h::49: error: declaration of int setresuid(uid_t, uid_t, uid_t) has a different exception specifier
int setresuid(uid_t ruid, uid_t euid, uid_t suid);
In file included from main.cxx::
/mnt/usr/include/unistd.h:: error: from previous declaration int setresuid(__uid_t, __uid_t, __uid_t) throw ()
extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid)
In file included from ../perlapi/perl.h::,
from ../perlapi/Utils.h:,
from main.cxx::
/archive/webql/build/al/current/base/dev/perl/core/perl.h: error: declaration of int setresgid(gid_t, gid_t, gid_t) has a different exception specifier
int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
In file included from main.cxx::
/mnt/usr/include/unistd.h:: error: from previous declaration int setresgid(__gid_t, __gid_t, __gid_t) throw ()
extern int setresgid (__gid_t __rgid, __gid_t __egid, __gid_t __sgid)
make: Leaving directory `/archive/webql/build/al/current/base/dev/webql'
Things tried
Re compiled the boost 1.42 and other dependencies
Expectation
Even after trying several things the error remains same. perl.h is part of project code while unistd.h is a system file. Obviously perl.h is C code hence it cannot THROW exception as present in unstd.h function declaration.
Any help would be appreciated.

'thread' was not declared in this scope [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

Thread was not declared in this scope error [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

Error: ‘to_string’ is not a member of ‘std’ when compiling in command line with gcc 4.8.2, works in NetBeans?

I am doing tests for my code with GoogleTest (1.7.0). I must have the tests compiling in both NetBeans (8.0.2) and straight from command line.
NetBeans has GoogleTest integrated as suggested here: https://youtu.be/TS2CTf11k1U.
I have also build GoogleTest from the command line (in different place) with the instructions provided with the package.
The problematic line of code is (a and b are doubles):
std::string input = std::to_string(a) + " " + std::to_string(b);
which gives error: ‘to_string’ is not a member of ‘std’, when compiling with
g++ -isystem -std=c++0x ~/gtest-1.7.0/include -pthread ~/Task1/test_source.cpp libgtest.a -o test_module1_1
as instructed in GoogleTest documentation.
The strange thing here is that when using the "Test" from NetBeans (as seen on video), the tests compile and run correctly.
I am using Ubuntu 14.04 and I have already tried to update g++ (not to a higher version though), and I have also tried specifying the version with -std=c++11 instead of -std=c++0x.
In NetBeans there is this warning visible:
Library File /usr/include/c++/4.8/string
but there is an unresolved #include <stddef.h>
in included /usr/include/_G_config.h
I found out that this might be because multilib and I have also tried to fix that by checking that it is installed. And this doesn't really cause any warnings during compile so it might be just that the IDE is confused.
But conserning the real problem about compile errors:
Is there something wrong with the GoogleTest framework, the compiler or my code?
PS. Preferably don't suggest that I just don't use to_string as this system must be usable by students studying C++11 and really it shouldn't be that they can't use all the functionality that should be supported.
I think your problem is the order in which you're passing command line arguments to gcc
g++ -isystem -std=c++0x ~/gtest-1.7.0/include ...
^^^^^^^^^^^^^^^^^^^
The -isystem flag should be followed by a path that contains headers which you want gcc to treat as system headers. So the -std=c++0x flag is being incorrectly consumed by the -isystem flag. You probably want the command line to be
g++ -std=c++0x -isystem ~/gtest-1.7.0/include ...