I'm new to c++, and I've started a project for my internship where I have use to the Snap library from stanford (http://snap.stanford.edu/). So I have downloaded the library and I am now trying to create my own little programm using it. Sadly i can't seem to be able to compile it :(
Here are the sources :
Makefile :
CXXFLAGS += -std=c++98 -Wall
LDFLAGS += -lrt
Snap.o :
g++ -c $(CXXFLAGS) ../snap/snap/Snap.cpp -I../snap/glib -I../snap/snap -pg
simulation.o : simulation.cpp simulation.h
g++ -g -c $(CXXFLAGS) simulation.cpp
test.o : test.cpp
g++ -g -c $(CXXFLAGS) test.cpp
test : test.o Snap.o simulation.o
g++ -g $(LDFLAGS) test.o Snap.o simulation.o -I../snap/glib -I../snap/snap -lm -o test
simulation.h
#ifndef SIMULATION
#define SIMULATION
#include <vector>
#include "../snap/snap/Snap.h"
class Simulation{
public:
Simulation():score(-1),nNodes(-1),nEdges(-1), dMax(-1){};
Simulation(int nN, int nE, int d);
Simulation(int d, PUNGraph g);
void setDMax(int d){ dMax = d; }
double getScore(){ return score; }
int getNNodes(){ return nNodes; }
int getNEdges(){ return nEdges; }
int getDMax(){ return dMax; }
PUNGraph getGraph(){ return graph; }
std::vector<int> getAlignment(){ return alignment; }
double computeEnergy();
private:
double score;
int nNodes;
int nEdges;
int dMax;
PUNGraph graph;
std::vector<int> alignment;
};
#endif
simulation.cpp
#include "simulation.h"
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include "../snap/snap/Snap.h"
Simulation::Simulation(int nN, int nE, int d){
nNodes = nNodes;
nEdges = nEdges;
dMax = dMax;
graph = TSnap::GenRdnGnm<PUNGraph>(nNodes,nEdges);
for(int i=1; i<=nNodes; i++){
alignment.push_back(i);
}
random_shuffle(alignment.begin(),alignment.begin()+nNodes);
computeEnergy();
}
Simulation::Simulation(int d, PUNGraph g){
nNodes = graph->GetNodes();
nEdges = graph->GetEdges();
dMax = d;
graph = g;
for(int i=1; i<=nNodes; i++){
alignment.push_back(i);
}
random_shuffle(alignment.begin(),alignment.begin()+nNodes);
computeEnergy();
}
double computeEnergy(){
return 0.0;
}
test.cpp
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include "simulation.h"
#include "../snap/snap/Snap.h"
int main(int argc, char** argv){
Simulation sim(5000,30000,30);
}
I don't think my compilation problems come from Snap itself and it might very well be only from my poor knowledge of c++ and how the includes an so on are working.
Here is what I get after running make :
g++ -g -c -std=c++98 -Wall simulation.cpp
In file included from /usr/include/c++/4.5/bits/stl_algo.h:61:0,
from /usr/include/c++/4.5/algorithm:63,
from simulation.cpp:5:
/usr/include/c++/4.5/bits/algorithmfwd.h:347:41: error: macro "max" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:358:41: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:343:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:347:5: error: template declaration of ‘const _Tp& std::max’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:354:5: error: expected initializer before ‘const’
/usr/include/c++/4.5/bits/algorithmfwd.h:358:5: error: template declaration of ‘const _Tp& std::min’
In file included from /usr/include/c++/4.5/algorithm:63:0,
from simulation.cpp:5:
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance)’:
/usr/include/c++/4.5/bits/stl_algo.h:3172:26: error: expected unqualified-id before ‘(’ token
/usr/include/c++/4.5/bits/stl_algo.h: In function ‘void std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance, _Compare)’:
/usr/include/c++/4.5/bits/stl_algo.h:3202:26: error: expected unqualified-id before ‘(’ token
simulation.cpp: In constructor ‘Simulation::Simulation(int, int, int)’:
simulation.cpp:11:13: error: ‘GenRdnGnm’ is not a member of ‘TSnap’
simulation.cpp:11:38: error: expected primary-expression before ‘>’ token
simulation.cpp:11:47: warning: left-hand operand of comma has no effect
I'd be very glad if some one could help resolve my problems (and if you feel like giving some c++/programming wisdom in the process i'd be even happier :) )
Ortholle
The Snap library headers contain the unfortunate macro definitions:
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
This will cause problems with code that uses (or defines) std::min and std::max.
You can get around this by making sure to include STL headers before Snap, or possibly by adding
#undef min
#undef max
after including the Snap.h header.
Another problem with your code: What's with all those extraneous #includes? Example: Your test.cpp #includes a whole bunch of stuff it doesn't need. All that test.cpp needs is (or should need) simulation.h. simulation.cpp has a similar problem with far too many #includes.
Don't #include something in a file that isn't used in that file.
(Aside: that random_shuffle in simulation.cpp should be std::random_shuffle).
None of these fixes are going to help with the base problem, which is that the Snap library 'conveniently' defines max and min as macros. You don't need these, so undef them.
Related
I'm trying to use Eigen3 to compute some complex eigenvalues and sofar everything worked, but know I get a weird error:
g++ -I/usr/include/eigen3/ -Icubature/ -I/usr/include/boost/ cubature/hcubature.c main.cpp wavefunction.cpp -o main.x
main.cpp: In function ‘int main(int, char**)’:
main.cpp:103:2: error: ‘ComplexEigenSolver’ was not declared in this scope
ComplexEigenSolver<MatrixXcd> ces;
^~~~~~~~~~~~~~~~~~
main.cpp:103:2: note: suggested alternative:
In file included from /usr/include/eigen3/Eigen/Eigenvalues:35:0,
from /usr/include/eigen3/Eigen/Dense:7,
from main.cpp:4:
/usr/include/eigen3/Eigen/src/Eigenvalues/ComplexEigenSolver.h:45:38: note: ‘Eigen::ComplexEigenSolver’
template<typename _MatrixType> class ComplexEigenSolver
^~~~~~~~~~~~~~~~~~
main.cpp:103:30: error: expected primary-expression before ‘>’ token
ComplexEigenSolver<MatrixXcd> ces;
^
main.cpp:103:32: error: ‘ces’ was not declared in this scope
ComplexEigenSolver<MatrixXcd> ces;
^~~
make: *** [Makefile:2: all] Error 1
Here are my includes:
#include <iostream>
#include <vector>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include "wavefunction.cpp"
#include "cubature.h"
using Eigen::MatrixXcd;
This is the routine where the error occurs:
MatrixXcd hop(5,5);
for(int m1 = -2; m1 <= 2; m1++){
for(int m2 = -2; m2 <= 2; m2++){
hop(m1+2, m2+2) = ....;
}
}
ComplexEigenSolver<MatrixXcd> ces;
Everything works fine, only the last line doesn't work. I don't see the difference between my code and the example given at the Eigen website.
What am I doing wrong?
I am not sure what happened to my system, but now when I try to include boost/any.hpp in a program test.cc
#include <iostream>
#include <boost/any.hpp>
int main(int argc, char *argv[])
{
std::cout << "hello" << std::endl;
return 0;
}
using g++ -o test test.cc, I get the following error:
In file included from test.cc:2:0:
/usr/include/boost/any.hpp: In function 'ValueType boost::any_cast(boost::any&)':
/usr/include/boost/any.hpp:278:52: error: 'if_' in namespace 'boost::mpl' does not name a template type
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
^ /usr/include/boost/any.hpp:278:55: error: expected unqualified-id before '<' token
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
^ /usr/include/boost/any.hpp:284:28: error: 'ref_type' does not name a type
return static_cast<ref_type>(*result);
Everything is fine when I remove the include to any.hpp. I am using boost-1.56.0 and gcc-4.9.3.
Things were compiling fine a week ago, but I am not sure what I might have updated to cause this error. Any help would be appreciated.
I'm having trouble getting the following simple example to work with SWIG 1.3.40 (and I also tried 1.3.31). The Foo structure comes through as a Python module as long as I don't wrap it in a namespace, but as soon as I do I get a compilation error in the generated test_wrap.c.
test.h:
#ifndef __TEST_H__
#define __TEST_H__
#define USE_NS 1
#if USE_NS
namespace ns {
#endif
struct Foo {
float a;
float b;
float func();
};
#if USE_NS
}
#endif
#endif
test.cpp
#include "test.h"
#if USE_NS
namespace ns {
#endif
float Foo::func()
{
return a;
}
#if USE_NS
}
#endif
test.i
%module test
%{
#include "test.h"
%}
%include "test.h"
I run the following commands for building a bundle on OSX 10.6.3:
swig -python test.i
g++ -c -m64 -fPIC test.cpp
g++ -c -m64 -fPIC -I/usr/local/include -I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Headers test_wrap.c
g++ -o _test.so -bundle -flat_namespace -undefined suppress test_wrap.o test.o -L/usr/local/lib -L/opt/local/lib -lpython2.6
This works, but only if I take out the namespace. I though SWIG handled namespaces automatically in simple cases like this. What am I doing wrong?
This is the error that I get - it looks like SWIG references a 'ns' and a 'namespace' symbol which are undefined.
test_wrap.c: In function ‘int Swig_var_ns_set(PyObject*)’:
test_wrap.c:2721: error: expected primary-expression before ‘=’ token
test_wrap.c:2721: error: expected primary-expression before ‘namespace’
test_wrap.c:2721: error: expected `)' before ‘namespace’
test_wrap.c:2721: error: expected `)' before ‘;’ token
test_wrap.c: In function ‘PyObject* Swig_var_ns_get()’:
test_wrap.c:2733: error: expected primary-expression before ‘void’
test_wrap.c:2733: error: expected `)' before ‘void’
In your test.i file, add a "using namespace ns" line after the #include. Without that, your swig wrapper code won't know to look for Foo in the "ns" namespace.
While building an existing code base on Mac OS using its native build setup I am getting some basic strange error while compilation phase.
Does any of you have any idea, as I have seen it's been discussed earlier as well in this forum without any good reason. I can not see any conflicting files being included.
But still I am unable to compile the code because this error appears.
Source are like the code given below and compilation error appears
$ cat a.h
#include <string>
#include <sstream>
namespace brijesh {
typedef std::string String;
template<class T>
String toString(T value) {
std::ostringstream buffer;
buffer << value;
return buffer.str();
}
$ cat b.h
#include "a.h"
namespace brijesh {
class Platform {
public:
static String getName();
};
}
$ cat b.cpp
#include "b.h"
namespace brijesh {
String Platform::getName()
{
String name = "UNKNOWN";
#ifdef LINUX
name = "linux";
#endif
#ifdef MACOSX
name = "Mac";
#endif
return name;
}
}
flags used for compilation
g++ -c -o test.o -DRELEASE_VERSION -ggdb -arch ppc -mmacosx-version-min=10.4 -pipe -fpermiss ive -nostdinc -nostdinc++ -isystem /Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3 .3 -I/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++ -I/Developer/SDKs/MacOS X10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/ppc-darwin -isystem /Developer/SDKs/MacOSX10.3.9. sdk/usr/include -F/Developer/SDKs/MacOSX10.3.9.sdk/System/Library/Frameworks -Wreturn-type -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -Wall -Wno-multichar -Wno-unk nown-pragmas -Wno-long-double -fconstant-cfstrings -MP -MMD x.cpp
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/bits/locale_facets.h: In constructor 'std::collate_byname<_CharT>::collate_byname(const char*, size_t)':
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/bits/locale_facets.h:1072: error: '_M_c_locale_collate' was not declared in this scope
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/ppc-darwin/bits/messages_members.h: In constructor 'std::messages_byname<_CharT>::messages_byname(const char*, size_t)':
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/ppc-darwin/bits/messages_members.h:79: error: '_M_c_locale_messages' was not declared in this scope
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits: At global scope:
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: 'float __builtin_huge_valf()' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: 'float __builtin_huge_valf()' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: 'float __builtin_nanf(const char*)' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: 'float __builtin_nanf(const char*)' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:900: error: field initializer is not constant
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:915: error: field initializer is not constant
It looks like you're trying to use OS X 10.3 developer tools (Xcode et al) and are trying to target OS X 10.4, which is obviously not going to work. Either change your build command to remove incompatible flags, such as -mmacosx-version-min=10.4, or upgrade to a more current version of OS X + Xcode + SDKs.
This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited