c++ unresolved identifier future - c++

I was learning how to multithread in c++ and I run into this problem. I've searched the internet and didn't found a satisfactory answer.
I'm pretty sure the library's directory is included.
This are my system specifications:
Oracle Virtual box 5.2.12
Debian 9
Netbeans 8.2
gcc and g++ 6.3.0
This are the errors
Cannot find include file "/usr/include/c++/6".
unable to resolve identifier future.
unable to resolve identifier get.
This is my tutorials code:
// async example
#include <iostream> // std::cout
#include <future> // std::async, std::future
// a non-optimized way of checking for prime numbers:
bool is_prime (int x) {
std::cout << "Calculating. Please, wait...\n";
for (int i=2; i<x; ++i) if (x%i==0) return false;
return true;
}
int main ()
{
// call is_prime(313222313) asynchronously:
std::future<bool> fut = std::async (is_prime,313222313);
std::cout << "Checking whether 313222313 is prime.\n";
// ...
bool ret = fut.get(); // waits for is_prime to return
if (ret) std::cout << "It is prime!\n";
else std::cout << "It is not prime.\n";
return 0;
}
And this is the building output:
cd '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: se entra en el directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/asynctutorial
make[2]: se entra en el directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++ -c -g -include /usr/include/c++/6.3.0 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
cc1plus: fatal error: /usr/include/c++/6.3.0: No existe el fichero o el directorio
compilation terminated.
nbproject/Makefile-Debug.mk:66: fallo en las instrucciones para el objetivo 'build/Debug/GNU-Linux/main.o'
make[2]: *** [build/Debug/GNU-Linux/main.o] Error 1
make[2]: se sale del directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
nbproject/Makefile-Debug.mk:59: fallo en las instrucciones para el objetivo '.build-conf'
make[1]: *** [.build-conf] Error 2
make[1]: se sale del directorio '/home/sulheru/NetBeansProjects/20180907/asyncTutorial'
nbproject/Makefile-impl.mk:39: fallo en las instrucciones para el objetivo '.build-impl'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 323ms)

You need the -pthread option in your compiler flags.
Example:
➜ g++ -std=c++11 -pthread async.cpp
➜ ./a.out
Checking whether 313222313 is prime.
Calculating. Please, wait...
It is prime!

Related

boost::python linking error (undefined reference to numpy::empty and numpy::detail::get_float_dtype<64>()) when using templates

I'm experiencing difficulties working with boost::python and boost::python::numpy, with python 2.7, boost 1.67.0, and Eigen3.3. My compiler is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0.
I'm trying to convert Eigen::Matrix types to python ndarray and back (vector_list.hpp):
#ifndef VECTOR_PYLIST_HH
#define VECTOR_PYLIST_HH
#include<iostream>
#include<vector>
#include<boost/python.hpp>
#include<Eigen/Dense>
#include<typeinfo>
#include<boost/python/numpy.hpp>
namespace myutil {
template<class T>
boost::python::list to_py_list(Eigen::Matrix<T, Eigen::Dynamic,Eigen::Dynamic> matrix)
{
boost::python::list list;
boost::python::list helplist;
if (matrix.cols() > 1) {
for (int j = 0; j < matrix.cols(); j++) {
helplist = to_py_list<T>(matrix.col(j));
list.append(helplist);
}
} else {
for (int i = 0; i < matrix.rows(); i++) {
list.append(matrix(i,0));
}
}
return list;
}
template<class T>
boost::python::numpy::ndarray to_py_array(Eigen::Matrix<T, Eigen::Dynamic,Eigen::Dynamic> matrix)
{
int cols = matrix.cols();
int rows = matrix.rows();
boost::python::tuple tu = boost::python::make_tuple(rows,cols);
boost::python::numpy::dtype dtype = boost::python::numpy::dtype::get_builtin<T>();
boost::python::numpy::ndarray arr = boost::python::numpy::empty(tu, dtype);
for (int i; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = matrix(i,j);
}
}
return arr;
}
#endif
My test programme (eigen_ndarray_eigen.cpp) looks as follows:
#include<vector_list.hpp>
#include<iostream>
#include<typeinfo>
int main()
{
Py_Initialize();
np::initialize();
Eigen::Matrix<double,3,2> m;
m << 1,2,3,4,5,6;
std::cout<<m<<std::endl;
boost::python::numpy::ndarray l = myutil::to_py_array<double>(m);
Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> n = myutil::to_eigen<double>(l);
std::cout<<n<<std::endl;
return 0;
}
And my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(myutil)
set(CMAKE_BUILD_TYPE "Debug")
set(BOOST_ROOT "$ENV{BOOST_ROOT}")
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
find_package(Boost 1.61.0 COMPONENTS python REQUIRED)
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(${Boost_INCLUDE_DIR} include)
add_executable(test_eigen_ndarray_eigen test/eigen_ndarray_eigen.cpp)
target_link_libraries(test_eigen_ndarray_eigen Eigen3::Eigen ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
I get an error when compiling:
[ 50%] Building CXX object CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o
[100%] Linking CXX executable test_eigen_ndarray_eigen
CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o:In function `boost::python::numpy::ndarray myutil::to_py_array<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1>)':
/home/usr/devel/myutil/include/vector_list.hpp:70: undefined reference to `boost::python::numpy::empty(boost::python::tuple const&, boost::python::numpy::dtype const&)'
CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o: In function `boost::python::numpy::detail::builtin_dtype<double, false>::get()':
/usr/include/boost/python/numpy/dtype.hpp:98: undefined reference to `boost::python::numpy::dtype boost::python::numpy::detail::get_float_dtype<64>()'
collect2: error: ld returned 1 exit status
CMakeFiles/test_eigen_ndarray_eigen.dir/build.make:96: recipe for target 'test_eigen_ndarray_eigen' failed
make[2]: *** [test_eigen_ndarray_eigen] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test_eigen_ndarray_eigen.dir/all' failed
make[1]: *** [CMakeFiles/test_eigen_ndarray_eigen.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
It's clearly a linking problem but I am already linking my executable against all boost libraries, and I can't figure out what might be wrong. Has anyone experienced something similar? Any tips?
Thanks for your input.
Edit: I was asked for the full error message when compiled with VERBOSE=1:
/usr/bin/cmake -H/home/usr/devel/myutil -B/home/usr/devel/myutil/build --check-build-system CMakeFiles/Makefile.cmake 1
/usr/bin/cmake -E cmake_progress_start /home/usr/devel/myutil/build/CMakeFiles /home/usr/devel/myutil/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/usr/devel/myutil/build'
make -f CMakeFiles/test_eigen_ndarray_eigen.dir/build.make CMakeFiles/test_eigen_ndarray_eigen.dir/depend
make[2]: Entering directory '/home/usr/devel/myutil/build'
cd /home/usr/devel/myutil/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/usr/devel/myutil /home/usr/devel/myutil /home/usr/devel/myutil/build /home/usr/devel/myutil/build /home/usr/devel/myutil/build/CMakeFiles/test_eigen_ndarray_eigen.dir/DependInfo.cmake --color=
Dependee "../include/vector_list.hpp" is newer than depender "CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o".
Clearing dependencies in "/home/usr/devel/myutil/build/CMakeFiles/test_eigen_ndarray_eigen.dir/depend.make".
Scanning dependencies of target test_eigen_ndarray_eigen
make[2]: Leaving directory '/home/usr/devel/myutil/build'
make -f CMakeFiles/test_eigen_ndarray_eigen.dir/build.make CMakeFiles/test_eigen_ndarray_eigen.dir/build
make[2]: Entering directory '/home/usr/devel/myutil/build'
[ 50%] Building CXX object CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o
/usr/bin/c++ -I/usr/include/python2.7 -I/home/usr/devel/myutil/include -isystem /home/usr/devel/src/eigen-git-mirror -g -o CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o -c /home/usr/devel/myutil/test/eigen_ndarray_eigen.cpp
[100%] Linking CXX executable test_eigen_ndarray_eigen
/usr/bin/cmake -E cmake_link_script CMakeFiles/test_eigen_ndarray_eigen.dir/link.txt --verbose=1
/usr/bin/c++ -g CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o -o test_eigen_ndarray_eigen -rdynamic -lboost_python -lpython2.7
CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o: In function `boost::python::numpy::ndarray myutil::to_py_array<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1>)':
/home/usr/devel/myutil/include/vector_list.hpp:70: undefined reference to `boost::python::numpy::empty(boost::python::tuple const&, boost::python::numpy::dtype const&)'
CMakeFiles/test_eigen_ndarray_eigen.dir/test/eigen_ndarray_eigen.cpp.o: In function `boost::python::numpy::detail::builtin_dtype<double, false>::get()':
/usr/include/boost/python/numpy/dtype.hpp:98: undefined reference to `boost::python::numpy::dtype boost::python::numpy::detail::get_float_dtype<64>()'
collect2: error: ld returned 1 exit status
CMakeFiles/test_eigen_ndarray_eigen.dir/build.make:96: recipe for target 'test_eigen_ndarray_eigen' failed
make[2]: *** [test_eigen_ndarray_eigen] Error 1
make[2]: Leaving directory '/home/usr/devel/myutil/build'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test_eigen_ndarray_eigen.dir/all' failed
make[1]: *** [CMakeFiles/test_eigen_ndarray_eigen.dir/all] Error 2
make[1]: Leaving directory '/home/usr/devel/myutil/build'
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
`'
I checked some answers regarding similar problems (here). Apparently boost_numpy doesn't call the right libraries automatically and sometimes you have to include them manually:
#define BOOST_LIB_NAME "boost_numpy"
#include <boost/config/auto_link.hpp>
I tried this but it didn't work for me...

Compilation error due to table in C++

I'm trying to create a chat programm for the TI-Nspire Calc via the serial port. So I installed the ndless SDK and nspireio lib to make the communication and it kind of works because there is an infite repetition of the message so I wrote this:
if(uart_ready()) {
char input[100] = {0};
uart_getsn(input,100);
if(oldinput != input) {
nio_puts(input);
oldinput = input;
}
}
But when I compile it give me this error:
root#Kali-Linux:~/TINSPIRE/Ndless/ndless-sdk/samples/uart# make
nspire-g++ -Wall -W -marm -Os -c hello.cpp
hello.cpp: Dans la fonction « int main() »:
hello.cpp:61:14: error: affectation de tableau invalide
oldinput = input;
^~~~~
Makefile:33 : la recette pour la cible « hello.o » a échouée
make: *** [hello.o] Erreur 1
What I'm doing wrong?
If oldinput is also a char array,
replace oldinput=input with
strcpy(oldinput,input);
You should declare oldinput:
char oldinput[100] = {0};
memcpy(oldinput, input, sizeof(char) * 100);

Error when using fstream in Netbeans/MingW

A while ago I installed MingW to learn C++ with NetBeans, but until now I didn't find the time. Now I created a new C++ project, made a Hello World program with std::cout and it worked immediately. Then I tried to write a file, but it failed:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
cout << "test" << endl;
fstream f;
f.open("a.txt", fstream::in | fstream::out | fstream::trunc);
f << "out" << endl;
f.close();
return 0;
}
lumosc (Build, run):
cd 'D:\<My name>\Programmierung\NetBeans Projects\lumosc_1\lumosc'
D:\Programme\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/D/Programme/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
"/D/Programme/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_1-Windows/lumosc.exe
make.exe[2]: Entering directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
mkdir -p build/Debug/MinGW_1-Windows
rm -f "build/Debug/MinGW_1-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/MinGW_1-Windows/main.o.d" -o build/Debug/MinGW_1-Windows/main.o main.cpp
mkdir -p dist/Debug/MinGW_1-Windows
g++ -o dist/Debug/MinGW_1-Windows/lumosc build/Debug/MinGW_1-Windows/main.o
make.exe[2]: Leaving directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
make.exe[1]: Leaving directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
Output (Note that cout << "test" << endl; wasn't executed):
RUN FAILED (exit value -1.073.741.511, total time: 64ms)
I didn't see any error message. However, when I started the newly created lumosc.exe manually, the following window popped up:
(Translated: Entry point not found - The procedure entry point "__gxx_personality_v0" was not found in the "libstdc++-6.dll" DLL.)
Here's some information about my MingW installation:
I'm completely new to this topic and I hope somebody can help me out!

Can't run make using C++ stdlib system call

I've got the following code in C++
if (should_run_make) {
std::string make = "make -C ";
make.append(outdir);
std::cout << "Make cmd is " << make << std::endl;
system(make.c_str());
}
This reports the following:
Make cmd is make -C /home/hamiltont/temp/ make: Entering directory
/home/hamiltont/temp' make: *** No targets. Stop.
make: Leaving directory/home/hamiltont/temp'
However, doing it manually works fine in multiple ways e.g.
[hamiltont#4 generator]$ make -C /home/hamiltont/temp/
make: Entering directory `/home/hamiltont/temp'
g++ -O3 -I/usr/include/openmpi-x86_64 -L/usr/local/lib -L/usr/lib64/openmpi/lib -lmpi -lmpi_cxx -lboost_serialization -lboost_mpi stg_impl.cpp -o impl
make: Leaving directory `/home/hamiltont/temp'
[hamiltont#4 generator]$ cd /home/hamiltont/temp/
[hamiltont#4 temp]$ make
g++ -O3 -I/usr/include/openmpi-x86_64 -L/usr/local/lib -L/usr/lib64/openmpi/lib -lmpi -lmpi_cxx -lboost_serialization -lboost_mpi stg_impl.cpp -o impl
Are you generating the makefile from within your C program? That's the only reason I could imagine would cause that specific error message.
make: *** No targets. Stop.
Reproducing the error
Here's how I could generate that message:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("Makefile", "w");
fputs("all:\n\techo Done.\n", fp);
system("make");
fclose(fp);
return 0;
}
This, predictably, prints:
make: *** No targets. Stop.
I say predictably because Makefile will be empty! This is because IO is buffered...
Fixed version
So, I close the file before calling system(), which flushes the buffer (fflush() would also do the trick):
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("Makefile", "w");
fputs("all:\n\techo Done.\n", fp);
fclose(fp);
system("make");
return 0;
}
Output:
echo Done.
Done.
I used C's IO functions for clarity, but the same rules apply to <iostream>.

FFmpeg libswresample linker error

I'm trying to set-up a function, using the ffmpeg c api, to re-sample a stream (AVStream format) to fixed (1channel - u8 sample) format.
I'm using ffmpeg libswresample library, but when I try to compile, I get a linker error that is strictly connected to swresample functions.
Let me post the code:
double *resample8(AVFrame *frame, int size, AVCodec* codec){
/* initialization of the output array */
double *out = new double[size];
/* create resampling context */
struct SwrContext *resampleContext;
resampleContext = swr_alloc();
if (!resampleContext) {
fprintf(stderr, "Could not allocate resampler context\n");
return NULL;
}
/* set options */
av_opt_set_int(resampleContext, "in_channel_layout", *(codec->channel_layouts), 0);
av_opt_set_sample_fmt(resampleContext, "in_sample_fmt", *(codec->sample_fmts), 0);
av_opt_set_int(resampleContext, "out_channel_layout", AV_CH_LAYOUT_MONO, 0);
av_opt_set_sample_fmt(resampleContext, "out_sample_fmt", AV_SAMPLE_FMT_U8, 0);
/* initialize the resampling context */
if (swr_init(resampleContext) < 0) {
fprintf(stderr, "Failed to initialize the resampling context\n");
return NULL;
}
return NULL;
}
Here is the inclusion:
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#ifdef __cplusplus
} // end extern "C".
#endif // __cplusplus
And here is the error I get, compiling with netbeans, g++
(additional g++ commands: -lavcodec -lavformat -lavutil -lswresample -lfftw3 -lm)
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: ingresso nella directory "/home/davide/Documenti/Tesi/audiosync-fin/audiosync"
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/audiosync
make[2]: ingresso nella directory "/home/davide/Documenti/Tesi/audiosync-fin/audiosync"
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/audiosync build/Debug/GNU-Linux-x86/dataReader.o build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/tansforms.o build/Debug/GNU-Linux-x86/tognuplot.o -lavcodec -lavformat -lavutil -lswresample -lfftw3 -lm
build/Debug/GNU-Linux-x86/dataReader.o: nella funzione "resample8(AVFrame*, int, AVCodec*)":
/home/davide/Documenti/Tesi/audiosync-fin/audiosync/dataReader.cpp:285: riferimento non definito a "av_opt_set_sample_fmt"
/home/davide/Documenti/Tesi/audiosync-fin/audiosync/dataReader.cpp:288: riferimento non definito a "av_opt_set_sample_fmt"
/usr/local/lib/libswresample.so: riferimento non definito a "av_calloc#LIBAVUTIL_54"
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:65: set di istruzioni per l'obiettivo "dist/Debug/GNU-Linux-x86/audiosync" non riuscito
make[2]: *** [dist/Debug/GNU-Linux-x86/audiosync] Errore 1
make[2]: uscita dalla directory "/home/davide/Documenti/Tesi/audiosync-fin/audiosync"
nbproject/Makefile-Debug.mk:62: set di istruzioni per l'obiettivo ".build-conf" non riuscito
make[1]: *** [.build-conf] Errore 2
make[1]: uscita dalla directory "/home/davide/Documenti/Tesi/audiosync-fin/audiosync"
nbproject/Makefile-impl.mk:39: set di istruzioni per l'obiettivo ".build-impl" non riuscito
make: *** [.build-impl] Errore 2
BUILD FAILED (exit value 2, total time: 5s)
here is the linker configuration linker config
I searched for any clue about the linker error, and I found that it could be caused by multiple versions of ffmpeg installed. I removed ubuntu (I'm using 15.04) base installation of ffmpeg and installed ffmpeg 2.7.2 from the tar downloaded by ffmpeg.org
I can't seem to find a solution at the moment, can anyone help?
I think the linking order matters
refer this question
Why does the order in which libraries are linked sometimes cause errors in GCC?
Try this order
libavdevice libavformat libavfilter libavcodec libwscale libavutil libswresample libswscale libpostproc libavresample