I want to do a fft in my c++ project, and show it afterwards as an image. In order to do the fft, I am using fftw++, and for displaying the image I wanted to use the CImg-library. Thus I started with the demo project from here. When compiling it, everything works. As soon as I add the CImg-header, it fails with the error
test.cpp: In function ‘int main()’:
test.cpp:18:12: error: ‘f’ was not declared in this scope
Complex *f=ComplexAlign(n);
My file looks like
#include "fftw++.h"
#include "CImg.h"
// Compile with:
// g++ -I .. -fopenmp example0.cc ../fftw++.cc -lfftw3 -lfftw3_omp
//using namespace std;
//using namespace utils;
//using namespace fftwpp;
//using namespace cimg_library;
int main()
{
fftwpp::fftw::maxthreads=get_max_threads();
std::cout << "1D complex to complex in-place FFT, not using the Array class"
<< std::endl;
unsigned int n=4;
Complex *f=utils::ComplexAlign(n);
fftwpp::fft1d Forward(n,-1);
fftwpp::fft1d Backward(n,1);
for(unsigned int i=0; i < n; i++) f[i]=i;
std::cout << "\ninput:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
Forward.fft(f);
std::cout << "\noutput:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
Backward.fftNormalized(f);
std::cout << "\ntransformed back:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
utils::deleteAlign(f);
}
and is compiled with
g++ -I .. -fopenmp test.cpp ../fftw++.cc -lfftw3 -lfftw3_omp
My g++ version is 4.8.5. Adding the Complex.h-header does not help either. What can I do in order to combine both libraries?
Edit: Further research shows that using the C-library complex.h and CImg.h results in a lot of compilation problems, combining the library Complex.h from the fftw++-package results also in errors, only the complex-include from C++ can be used together with the CImg.h-include file. Reason: Unknown till now.
My solution (even if it is not perfect) is, that I created a second cpp-file:
second.cpp:
#include "CImg.h"
//Code for images
void example(void){
}
and an include-file for that:
second.h:
#ifndef SECOND_H
#define SECOND_H
void example(void);
#endif /* SECOND_H */
If I only include that include file instead of CImg.h, I can use both fftw++ and CImg.
Related
I have a c++ program where I need to pass the square root of a number in a for loop.
#include<random>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <omp.h>
using namespace std;
int main()
{
vector<int>inputDataBits(49); // vector of randomly generated input data bits
#ifdef printDebug
std::cout << "the input data bits are" << endl;
std::cout << "-------------------------" << endl << endl;
int var =49;
const int r=(int)sqrt(var);
float input2d[r][r];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
input2d[i][j] = inputDataBits[(j %r) + (i *r)];
std::cout << input2d[i][j] << "\t";
}
std::cout << endl << endl;
}
std::cout << endl << endl;
#endif
return 0;
}
I get an error 'expression must have a constant value'. Is there a way to do this in c++?
This is the purpose of the constexpr keyword (make the value known at compile time).
constexpr int var=49;
constexpr int r=(int)sqrt(var);
Unfortunately, in the documentation sqrt() is not declared as a constexpr function.
Only gcc seems to consider it as constexpr but it is not portable.
The size of an array needs to be known at compile-time.
Instead you can use a std::vector, which has a dynamic size.
std::vector<std::vector<float>> input2d(std::vector<float>(r), r);
Here is a small program that incorporates a call to an Intel MKL library function and DLIB's optimization routine find_min_using_approximate_derivatives.
The code runs perfectly when compiled with the intel C++ compiler: icpc using the invocation:
icpc main.cpp -I /Users/Username/Code/dlib-18.16 -DDLIB_USE_BLAS -I$MKLROOT/include -L$MKLROOT/lib/ -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_lp64 -DENABLE_DLIB -DDLIB_USE_BLAS
or by disabling the DLIB related portion of the code via:
clang++ main.cpp -I$MKLROOT/include -L$MKLROOT/lib/ -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_lp64
C++ code
// #define ENABLE_DLIB
#ifdef ENABLE_DLIB
#include "dlib/optimization.h"
#endif
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "mkl.h"
using namespace std;
#ifdef ENABLE_DLIB
using namespace dlib;
#endif
template<typename T>
void printArray(T *data, string name, int len){
cout << name << "\n";
for(int i=0;i<len;i++){
cout << data[i] << " ";
}
cout << "\n";
}
#ifdef ENABLE_DLIB
typedef matrix<double,0,1> column_vector;
double rosen (const column_vector& m)
{
const double x = m(0);
const double y = m(1);
return 100.0*pow(y - x*x,2) + pow(1 - x,2);
}
#endif
int main()
{
#ifdef ENABLE_DLIB
column_vector starting_point(2);
starting_point = 4, 8;
find_min_using_approximate_derivatives(bfgs_search_strategy(),
objective_delta_stop_strategy(1e-7),
rosen, starting_point, -1);
cout << "\nBFGS rosen minimum lies at:\n";
cout << "x = " << starting_point(0) << endl;
cout << "y = " << starting_point(1) << endl;
#endif
int len=10;
double *x=new double[len];
double *y=new double[len];
for(int i=0;i<len;i++){
x[i]=(double)std::rand()/RAND_MAX;
y[i]=(double)std::rand()/RAND_MAX;
}
printArray<double>(x, "x", len);
printArray<double>(y, "y", len);
//sum(x)
double x_sum=cblas_dasum(len,x,1);
cout<< "sum(x): "<< x_sum <<"\n";
}
Nevertheless, it fails replacing icpc with clang++ above with multiple errors of the following kind:
Error
In file included from /opt/intel/composer_xe_2015.3.187/mkl/include/mkl.h:48:
/opt/intel/composer_xe_2015.3.187/mkl/include/mkl_cblas.h:584:6: error: conflicting types for 'cblas_cgemm'
void cblas_cgemm(const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE TransA,
^
/Users/Username/Code/dlib-18.16/dlib/matrix/matrix_blas_bindings.h:75:18: note: previous declaration is here
void cblas_cgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,
...suggesting a conflict between the cblas_* routines mirrored in MKL. The documentation for DLIB suggests using the DLIB_USE_BLAS preprocessor directive in order for it to link with MKL but evidently it doesn't seem to help using clang++.
How do I make this work?
I have built the facebook folly libraries by using the following link given below
https://github.com/facebook/folly
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <typeinfo>
#include <vector>
#include "folly/FBVector.h"
int main()
{
folly::fbvector<int> vec;
folly::fbvector<int>::iterator vIter;
int i = 0;
for(i = 0;i < 10;i++)
{
vec.push_back(i);
}
for(i = 0 ; i <=vec.size();i++)
{
cout << vec[i] << endl;
}
vec.erase(vec.begin()+ 5);
vec.insert(vec.begin()+5,25);
vec.insert(vec.begin()+5,5);
vec.insert(vec.begin()+5,5);
sort(vec.begin(),vec.end());
cout << "Using Iteration Concept " << endl;
for(vIter = vec.begin() ; vIter != vec.end();vIter++)
{
cout << *vIter << endl;
}
return 0;
}
After writting this code ,just compiled it..when i was compiling,getting the following issue..
syscon#syscon-OptiPlex-3020:~/Documents/work/folly/folly-master$ g++ -o stl stl.cpp -std=c++11 -lboost_system
/tmp/cc2WeDlr.o: In function folly::usingJEMalloc()':stl.cpp:(.text._ZN5folly13usingJEMallocEv[_ZN5folly13usingJEMallocEv]+0x2d): undefined reference tofolly::usingJEMallocSlow()'
collect2: error: ld returned 1 exit status
which library i need to include inorder to solve this issue?
That particular symbol comes from: https://github.com/facebook/folly/blob/master/folly/Malloc.cpp so it's part of the compiled folly library (ie, it's not just a header-only dependency). You need to link against it.
Depending on how you compiled/installed folly will determine how to fix the problem, e.g. adding -lfolly to the compilation line.
I want to define a vector with static content in a function that might be used by multiple programs. However I am facing some problems.
The main.cpp looked like this:
#include <iostream>
#include <boost/assign/std/vector.hpp>
using namespace std;
using namespace boost::assign;
int main (int argc, char* argv[]) {
vector< int > v;
v += 3,5,1;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
This works and I get as an output:
v: 0 3
v: 1 5
v: 2 1
However if I try to put the vector definition into a separate file it does not work.
main.cpp
#include <iostream>
#include "vector_test.hpp"
using namespace std;
int main (int argc, char* argv[]) {
vector< int > v;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
vector_test.hpp:
#include <boost/assign/std/vector.hpp>
#include <stdio.h>
using namespace std;
using namespace boost::assign;
static std::vector<int> v;
v += 3,5,1;
Trying to compile this gives me an error:
'v' does not name a type
and the qt creator also tells me:
expected a declaration
How do I fix this?
One way would be to have a function returning a reference to a static instance:
const std::vector<int>& get_magic_vector()
{
static std::vector<int> v{3, 5, 1};
return v;
}
Then
for (auto i : get_magic_vector())
std::cout << i << " ";
In your other files you must say that the variable exists but is defined in another file. That is, in otherFile.cpp you place the following line at file scope:
extern std::vector<int> v;
/*
* File: main.cpp
* Author: c1004527
*
* Created on February 15, 2012, 10:25 PM
*/
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include "CardClass.h"
#include "PlayerClass.h"
/*
*
*/
int main(int argc, char** argv)
{
srand(time(0));
CardClass deck;
PlayerClass player[4];
deck.ShuffleCards();
deck.Print();
for(int i = 0; i < DECK_SIZE; i++)
{
player[i%4].AddACard(deck.DealCards());
}
for(int i = 0; i < 4; i++)
{
player[i].SortCard();
}
for(int i = 0; i < 4; i++)
{
cout << endl << endl
<< "Player "<< i+1 << endl
<< player[i];
}
return (0);
}
The error is:
**** Internal Builder is used for build
**** g++ -O0 -g3 -Wall -c -fmessage-length=0 -o OLA3\OLA3\main.o ..\OLA3\OLA3\main.cpp
..\OLA3\OLA3\main.cpp: In function 'int main(int, char**)':
..\OLA3\OLA3\main.cpp:17:17: error: 'time' was not declared in this scope
Build error occurred, build is stopped
Time consumed: 114 ms.
Everything else compiles completely fine but that. I use minGW and it compiles hello world with no problem.
Because you've included <ctime>, time is going to be defined in the std namespace.
Therefore, you need to use a fully-qualified reference: std::time.
That's what this error is telling you:
error: 'time' was not declared in this scope