I have tried to get following sample code working to know whether asynchronous programming is working in Android NDK. Though NDK has the STL <future> which gets recognized as a header, the std::async is not getting recognized are not getting recognized. The code I tried to use was the following:
#include <future>
#include <iostream>
struct Foo
{
Foo() : data(0) {}
void sum(int i) { data +=i;}
int data;
};
int main()
{
Foo foo;
auto f = std::async(&Foo::sum, &foo, 42);
f.get();
std::cout << foo.data << "\n";
}
Also all the include paths have been set to the specified folder under Properties->Paths and Symbols
Errors
Description Resource Path Location Type
invalid use of incomplete type 'std::__async_sfinae_helper<void (Foo::*)(int), void (Foo::*)(int), Foo*, int>::type {aka struct std::future<void>}' Sample.cpp /Project12/jni line 50 C/C++ Problem
Description Resource Path Location Type
declaration of 'std::__async_sfinae_helper<void (Foo::*)(int), void (Foo::*)(int), Foo*, int>::type {aka struct std::future<void>}' Project12 line 111, external location: D:\android-ndk-r8e-windows-x86_64\android-ndk-r8e\sources\cxx-stl\gnu-libstdc++\4.6\include\future C/C++ Problem
Curently Android NDK does not incorporate all of the C++11 features. Clang 3.3 compiler from NDK r9b is C++11-feature complete, however, STL and stdlib on Android are not.
To use the most recent C++11 feature set in Android use Clang 3.3 compiler from Android NDK r9b. Put this line into your Application.mk file:
NDK_TOOLCHAIN_VERSION := clang
Also, add -std=c++11 switch to the LOCAL_CPPFLAGS variable:
LOCAL_CPPFLAGS += -std=c++11
Related
Update:
These errors appear to be from CODAN, Eclipse's code analyzer, that appear in the Problems view and are also underlined in red on the line the error refers to. Oddly enough building the project successfully creates the executable, and the build does not report these errors in the Console. I can run that executable and get the expected output.
So now the question becomes: how to have Eclipse's CODAN recognize these uses of lambdas and function pointers with std::function are not errors?
Original Question:
In the following C++ code, it compiles fine directly with g++, but causes errors in Eclipse CDT. How can I get my Eclipse project to recognize and allow the use of lambdas (or function pointers) with std::function?
Note: see Edit at bottom of question for update
#include <functional>
#include <iostream>
void foo(int i) {
std::cout << i << std::endl;
}
void function_ptr(void (*bar)(int)) {
bar(1);
}
void function_std(std::function<void(int)> baz) {
baz(2);
}
int main() {
// these function calls are ok
function_ptr(&foo);
function_ptr([](int i) -> void {
foo(i);
});
// these function calls cause errors
function_std(&foo);
function_std([](int i) -> void {
foo(i);
});
// these assignments cause errors
std::function<void(int)> f1 = foo;
std::function<void(int)> f2 = [](int i) -> void {
foo(i);
};
f1(3);
f2(3);
return 0;
}
Compiling this code on the command line works as expected and produces the following output:
$ g++ src/LambdaFunctionParameterExample.cpp -o example
$ ./example
1
1
2
2
3
3
However in Eclipse, the function calls accepting a std::function as a parameter produce this error:
Invalid arguments '
Candidates are:
void function_std(std::function<void (int)>)
'
And the std::function variable assignments produce this error:
Invalid arguments '
Candidates are:
function()
function(std::nullptr_t)
function(const std::function<void (int)> &)
function(std::function<void (int)> &&)
function(#10000)
'
I have set the language standard to ISO C++17 (-std=c++17) in Project -> Properties -> C/C++ Build -> GCC C++ Compiler -> Dialect. (I have assumed setting this property was necessary to access the <functional> header according to this documentation. Oddly enough, specifying the language level (or not) is not effecting the above errors. And, specifying the language level is not necessary when building directly with g++.)
I'm using macOS Catalina 10.15.5, gcc 10.2.0 from homebrew, and Eclipse CDT version 2020-09 (4.17.0).
This behavior is a known bug with Eclipse CDT version 10.0.
Update:
This issue is fixed by upgrading to Eclipse CDT version 10.1:
https://download.eclipse.org/tools/cdt/builds/10.1/cdt-10.1.0-rc2/
Steps to fix:
Enter the above URL in Help --> Install New Software... --> Work with: and install all the options.
Restart Eclipse.
Select Project --> C/C++ Index --> Rebuild.
I have the following very simple program that seems to fail on the Intel compiler but not gcc when using boost::lexical_cast (boost version 1.67.0):
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
float getSomeNumber()
{
return 10.0;
}
int main(int argc, char** argv)
{
std::string mystring("Hello: " + boost::lexical_cast<std::string>(getSomeNumber());
std::cout << mystring << std::endl;
return 0;
}
When compiling, Intel returns the following error:
/boost/1.67.0/include/boost/type_traits/is_complete.hpp(45): warning #70: incomplete type is not allowed
ok_tag<sizeof(T)> check_is_complete(int);
detected during ....
...
/boost/1.67.0/include/boost/type_traits/is_complete.hpp(51): error: template instantiation resulted in unexpected function type of "boost::detail::ok_tag<1U> (int)" (the meaning of a name may have changed since the template declaration -- the type of the template is "boost::detail::ok_tag<sizeof(T)> (int)")
...
detected during:
instantiation of "boost::detail::check_is_complete" based on template argument <void> at line 51
instantiation of class "boost::is_complete<T> [with T=void]" at line 484 of "/Path/to/boost/include/boost/type_traits/is_convertible.hpp"
...
instantiation of "Target boost::lexical_cast<Target, Source>(const Source &) [with Target=std::string, Source=float]" at line 11 of "boostTest.cc"
The best I can tell from this error is it seems like the compiler can't complete the template resolution, but then the very end it seems like it does? I tried compiling the same thing under gcc (version 4.8.5) and it worked fine. This code also previously worked fine under boost 1.64.0 with both gcc and Intel.
Is this a bug in the Intel Compiler, or perhaps a bug in boost? Is there a way to modify my program as a workaround?
Software versions
gcc: 4.8.5
Intel Compiler: 2015.3.187
Boost: 1.67.0
OS: RHEL 7.5 (Maipo)
Compile line:
icpc/gcc --std=c++11 boostTest.cc -o boostTest -Ipath/to/boost/include -lstdc++
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem
MinGw 4.7.0 Boost 1.47
code:
#include <iostream>
#include <boost/thread.hpp>
int main(int argc, char *argv[])
{
std::cout<<"In main"<<std::endl;
}
Compiler string:
g++.exe -Wall -fexceptions -g -IC:\soft\ides_comp\mingw\include\boost_1_47_0 -c D:\work\cpp_cb\mt1\main.cpp -o obj\Debug\main.o
It doesnt compile and i have same error whatever compiler options i tried.
Warning:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:12:0,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13,
from D:\work\cpp_cb\mt1\main.cpp:2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59:40: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as dllimport: attribute ignored [-Wattributes]
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69:39: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as dllimport: attribute ignored [-Wattributes]
Error:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:30:0,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/shared_ptr.hpp:17,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/date_time/time_clock.hpp:17,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread_time.hpp:9,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:10,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13,
from D:\work\cpp_cb\mt1\main.cpp:2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp: In instantiation of 'void boost::checked_delete(T*) [with T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]':
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/detail/shared_count.hpp:95:13: required from 'boost::detail::shared_count::shared_count(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:183:50: required from 'boost::shared_ptr<T>::shared_ptr(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>; T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:171:69: required from 'const E& boost::exception_detail::set_info(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:192:46: required from 'typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type boost::operator<<(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*; typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type = const boost::unknown_exception&]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:182:13: required from 'void boost::unknown_exception::add_original_type(const E&) [with E = std::exception]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:161:32: required from here
Warning 2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp:34:5: warning: deleting object of polymorphic class type 'boost::error_info<boost::tag_original_exception_type, const std::type_info*>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
What can it be? Thanks
This has been fixed in later boost versions. Check here:
https://svn.boost.org/trac/boost/ticket/6165
Update boost/config/stdlib/libstdcpp3.hpp:
#if defined(_GLIBCXX_HAVE_GTHR_DEFAULT) \
|| defined(_GLIBCXX_HAS_GTHREADS) \
|| defined(_GLIBCXX__PTHREADS) \
|| defined(WIN32)
Added _GLIBCXX_HAS_GTHREADS and/or WIN32 (from the link above, see patch).
WIN32 seems a bit brute force, but doesn't work without (at least not for me. I am using an older boost version 1.42).
If you compiled the thread library for using Win32 threads, you might need
-DBOOST_USE_WINDOWS_H
in your compiler flags. While I don't think that's the issue (I get a different set of errors for not using it), you might give it a try.
This answer maybe not very straight, but:
I do use g++ 4.7.0 20111209 and it has support for threads.
#include <iostream>
#include <thread>
using namespace std;
void foo()
{
cout << "Hello from thread\n";
}
int main()
{
thread th1(foo);
th1.join();
return 0;
}
During compilation use -std=c++11 option to use the latest standard which supports threads
PS:
boost 1.48 compiled on my pc with mingw 4.7.0 gives an error (when trying to use boost::thread):
Threading support unavailable: it has been explicitly disabled with BOOST_DISABLE_THREADS
So I guess it was disabled by default with g++ 4.7.0 (with g++ 4.6.2 it was not).
I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.