Question

I can't get my C++ program to compile. As you can see, I'm writing a Node.js addon. Here is the code (Eamorr_addon.cpp):

#include <iostream>
#include <v8.h>
#include <node.h>
#include <gmp.h>

#include "Definitions.h"
#include "Rk.h"

#include "tbb/concurrent_hash_map.h"
//#include <tbb/concurrent_vector.h>

using namespace std;
using namespace v8;



static Handle<Value> Echo(const Arguments& args) {
    HandleScope scope;

    if (args.Length() < 1) {
        return ThrowException(Exception::TypeError(String::New("Bad argument")));
    }

    return scope.Close(args[0]);
}

extern "C" void init (Handle<Object> target)
{
    HandleScope scope;
    target->Set(String::New("hello"), String::New("world"));

    NODE_SET_METHOD(target, "echo", Echo);
}

Everything works just fine when I remove the line #include "tbb/concurrent_hash_map.h".

Here is my gyp make file:

{
  'targets': [
    {
      'target_name':'Eamorr_addon',
      'sources':['src/Eamorr_addon.cpp'],
      'include_dirs': ['/usr/include','/usr/local/include/'],
      'cflags': ['-Wall','-fopenmp'],
      'ldflags': ['-ltbb','-lgmp']
    }
  ]
}

When I do node-gyp build, I get the following stream of errors which I can't get to the bottom of:

node-gyp build
info it worked if it ends with ok 
spawn make [ 'BUILDTYPE=Release', '-f', 'Makefile.gyp' ]
  CXX(target) out/Release/obj.target/DeDuplicator_addon/src/DeDuplicator_addon.o
In file included from /usr/include/tbb/concurrent_hash_map.h:51:0,
                 from src/DeDuplicator_addon.cpp:17:
/usr/include/tbb/atomic.h:218:31: error: expected nested-name-specifier before numeric constant
/usr/include/tbb/atomic.h:218:31: error: expected ‘>’ before numeric constant
/usr/include/tbb/atomic.h:224:33: error: expected ‘)’ before ‘addend’
/usr/include/tbb/atomic.h:224:40: error: ISO C++ forbids initialization of member ‘fetch_and_add’
/usr/include/tbb/atomic.h:224:40: error: making ‘fetch_and_add’ static
/usr/include/tbb/atomic.h:224:40: error: template declaration of ‘value_type tbb::internal::fetch_and_add’
In file included from /usr/include/tbb/concurrent_hash_map.h:51:0,
                 from src/DeDuplicator_addon.cpp:17:
/usr/include/tbb/atomic.h:228:31: error: expected identifier before numeric constant
/usr/include/tbb/atomic.h:228:31: error: expected ‘,’ or ‘...’ before numeric constant
/usr/include/tbb/atomic.h:251:28: error: expected identifier before numeric constant
/usr/include/tbb/atomic.h:251:28: error: expected ‘,’ or ‘...’ before numeric constant
/usr/include/tbb/atomic.h:255:28: error: expected identifier before numeric constant
/usr/include/tbb/atomic.h:255:28: error: expected ‘,’ or ‘...’ before numeric constant
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::fetch_and_add(int)’:
/usr/include/tbb/atomic.h:229:48: error: ‘addend’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::fetch_and_decrement()’:
/usr/include/tbb/atomic.h:243:33: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:243:33: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:243:33: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::fetch_and_decrement()’:
/usr/include/tbb/atomic.h:247:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:247:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:247:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::operator+=(int)’:
/usr/include/tbb/atomic.h:252:30: error: ‘addend’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::operator-=(int)’:
/usr/include/tbb/atomic.h:258:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:258:32: error: ‘addend’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::operator--()’:
/usr/include/tbb/atomic.h:266:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:266:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:266:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h: In member function ‘value_type tbb::internal::atomic_impl_with_arithmetic<I, <anonymous> >::operator--(int)’:
/usr/include/tbb/atomic.h:274:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:274:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h:274:30: error: ‘2’ cannot be used as a function
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:329:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘long long int tbb::atomic<long long int>::operator=(long long int)’:
/usr/include/tbb/atomic.h:329:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<long long int>& tbb::atomic<long long int>::operator=(const tbb::atomic<long long int>&)’:
/usr/include/tbb/atomic.h:329:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:330:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘long long unsigned int tbb::atomic<long long unsigned int>::operator=(long long unsigned int)’:
/usr/include/tbb/atomic.h:330:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<long long unsigned int>& tbb::atomic<long long unsigned int>::operator=(const tbb::atomic<long long unsigned int>&)’:
/usr/include/tbb/atomic.h:330:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:335:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘long int tbb::atomic<long int>::operator=(long int)’:
/usr/include/tbb/atomic.h:335:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<long int>& tbb::atomic<long int>::operator=(const tbb::atomic<long int>&)’:
/usr/include/tbb/atomic.h:335:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:336:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘long unsigned int tbb::atomic<long unsigned int>::operator=(long unsigned int)’:
/usr/include/tbb/atomic.h:336:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<long unsigned int>& tbb::atomic<long unsigned int>::operator=(const tbb::atomic<long unsigned int>&)’:
/usr/include/tbb/atomic.h:336:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:352:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘unsigned int tbb::atomic<unsigned int>::operator=(unsigned int)’:
/usr/include/tbb/atomic.h:352:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<unsigned int>& tbb::atomic<unsigned int>::operator=(const tbb::atomic<unsigned int>&)’:
/usr/include/tbb/atomic.h:352:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:353:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘int tbb::atomic<int>::operator=(int)’:
/usr/include/tbb/atomic.h:353:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<int>& tbb::atomic<int>::operator=(const tbb::atomic<int>&)’:
/usr/include/tbb/atomic.h:353:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:356:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘short unsigned int tbb::atomic<short unsigned int>::operator=(short unsigned int)’:
/usr/include/tbb/atomic.h:356:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<short unsigned int>& tbb::atomic<short unsigned int>::operator=(const tbb::atomic<short unsigned int>&)’:
/usr/include/tbb/atomic.h:356:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:357:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘short int tbb::atomic<short int>::operator=(short int)’:
/usr/include/tbb/atomic.h:357:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<short int>& tbb::atomic<short int>::operator=(const tbb::atomic<short int>&)’:
/usr/include/tbb/atomic.h:357:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:358:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘char tbb::atomic<char>::operator=(char)’:
/usr/include/tbb/atomic.h:358:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<char>& tbb::atomic<char>::operator=(const tbb::atomic<char>&)’:
/usr/include/tbb/atomic.h:358:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:359:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘signed char tbb::atomic<signed char>::operator=(signed char)’:
/usr/include/tbb/atomic.h:359:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<signed char>& tbb::atomic<signed char>::operator=(const tbb::atomic<signed char>&)’:
/usr/include/tbb/atomic.h:359:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:360:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘unsigned char tbb::atomic<unsigned char>::operator=(unsigned char)’:
/usr/include/tbb/atomic.h:360:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<unsigned char>& tbb::atomic<unsigned char>::operator=(const tbb::atomic<unsigned char>&)’:
/usr/include/tbb/atomic.h:360:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:363:1: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
/usr/include/tbb/atomic.h: In member function ‘wchar_t tbb::atomic<wchar_t>::operator=(wchar_t)’:
/usr/include/tbb/atomic.h:363:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: In member function ‘tbb::atomic<wchar_t>& tbb::atomic<wchar_t>::operator=(const tbb::atomic<wchar_t>&)’:
/usr/include/tbb/atomic.h:363:1: error: ‘store_with_release’ was not declared in this scope
/usr/include/tbb/atomic.h: At global scope:
/usr/include/tbb/atomic.h:367:93: error: wrong number of template arguments (3, should be 2)
/usr/include/tbb/atomic.h:219:8: error: provided for ‘template<class I, int <anonymous> > struct tbb::internal::atomic_impl_with_arithmetic’
In file included from src/DeDuplicator_addon.cpp:17:0:
/usr/include/tbb/concurrent_hash_map.h: In member function ‘bool tbb::interface4::internal::hash_map_base::check_mask_race(tbb::interface4::internal::hash_map_base::hashcode_t, tbb::interface4::internal::hash_map_base::hashcode_t&) const’:
/usr/include/tbb/concurrent_hash_map.h:266:21: error: cannot convert ‘const tbb::atomic<long unsigned int>’ to ‘tbb::interface4::internal::hash_map_base::hashcode_t’ in assignment
/usr/include/tbb/concurrent_hash_map.h: In member function ‘tbb::interface4::internal::hash_map_base::segment_index_t tbb::interface4::internal::hash_map_base::insert_new_node(tbb::interface4::internal::hash_map_base::bucket*, tbb::interface4::internal::hash_map_base::node_base*, tbb::interface4::internal::hash_map_base::hashcode_t)’:
/usr/include/tbb/concurrent_hash_map.h:296:30: error: no match for ‘operator++’ in ‘++((tbb::interface4::internal::hash_map_base*)this)->tbb::interface4::internal::hash_map_base::my_size’
/usr/include/tbb/concurrent_hash_map.h: In member function ‘void tbb::interface4::internal::hash_map_base::reserve(tbb::interface4::internal::hash_map_base::size_type)’:
/usr/include/tbb/concurrent_hash_map.h:316:32: error: no match for ‘operator!’ in ‘!((tbb::interface4::internal::hash_map_base*)this)->tbb::interface4::internal::hash_map_base::my_size’
/usr/include/tbb/concurrent_hash_map.h:316:32: note: candidate is: operator!(bool) <built-in>
/usr/include/tbb/concurrent_hash_map.h:317:32: error: cannot convert ‘tbb::atomic<long unsigned int>’ to ‘tbb::interface4::internal::hash_map_base::size_type’ in initialization
/usr/include/tbb/concurrent_hash_map.h:317:58: error: cannot convert ‘tbb::atomic<long unsigned int>’ to ‘tbb::interface4::internal::hash_map_base::size_type’ in assignment
/usr/include/tbb/concurrent_hash_map.h: In member function ‘bool tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::empty() const’:
/usr/include/tbb/concurrent_hash_map.h:833:44: error: no match for ‘operator==’ in ‘((const tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>*)this)->tbb::interface4::internal::hash_map_base::my_size == 0’
/usr/include/tbb/concurrent_hash_map.h: In member function ‘tbb::interface4::concurrent_hash_map::size_type tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::bucket_count() const’:
/usr/include/tbb/concurrent_hash_map.h:839:53: error: no match for ‘operator+’ in ‘((const tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>*)this)->tbb::interface4::internal::hash_map_base::my_mask + 1’
/usr/include/tbb/concurrent_hash_map.h: In member function ‘bool tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::exclude(tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::const_accessor&, bool)’:
/usr/include/tbb/concurrent_hash_map.h:1119:16: error: no ‘operator--(int)’ declared for postfix ‘--’
/usr/include/tbb/concurrent_hash_map.h: In member function ‘bool tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::erase(const Key&)’:
/usr/include/tbb/concurrent_hash_map.h:1160:16: error: no ‘operator--(int)’ declared for postfix ‘--’
/usr/include/tbb/concurrent_hash_map.h: In member function ‘void tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::internal_copy(const tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>&)’:
/usr/include/tbb/concurrent_hash_map.h:1310:20: error: no match for ‘operator==’ in ‘((tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>*)this)->tbb::interface4::internal::hash_map_base::my_mask == mask’
/usr/include/tbb/concurrent_hash_map.h:1323:19: error: no match for ‘operator++’ in ‘++((tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>*)this)->tbb::interface4::internal::hash_map_base::my_size’
/usr/include/tbb/concurrent_hash_map.h: In member function ‘void tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>::internal_copy(I, I)’:
/usr/include/tbb/concurrent_hash_map.h:1340:11: error: no match for ‘operator++’ in ‘++((tbb::interface4::concurrent_hash_map<Key, T, HashCompare, A>*)this)->tbb::interface4::internal::hash_map_base::my_size’
make: *** [out/Release/obj.target/DeDuplicator_addon/src/DeDuplicator_addon.o] Error 1
ERR! Error: `make` failed with exit code: 2
    at Array.0 (/usr/local/lib/node_modules/node-gyp/lib/build.js:118:25)
    at EventEmitter._tickCallback (node.js:192:40)
ERR! not ok

Can anyone suggest anything?


Edit:

Here is "Rk.h":

#ifndef RK_H_
#define RK_H_

#include <gmp.h>
#include "Definitions.h"

unsigned long int init_rkHash(char* str);
unsigned long int get_rkHash(unsigned long int prevHash,int prevAscii,int topAscii);
/*unsigned long int getFirstPrevHash(char* &buffer);*/

#endif /* RK_H_ */

and here is "Definitions.h":

#ifndef DEFINITIONS_H_
#define DEFINITIONS_H_

#define PORT 10002
#define CHUNKSIZE 512
#define PRIME 9223372036854775783
#define D 2


#endif /* DEFINITIONS_H_ */
Was it helpful?

Solution

#define macros are easy to use wrong. And giving them names like D calls for problems.

You defined D as

#define D 2

Now, if we look at atomic.h, we find e.g.

...
template<typename I, typename D, typename StepType>
struct atomic_impl_with_arithmetic: atomic_impl<I> {
...
    value_type fetch_and_add( D addend ) {
...
    template<memory_semantics M>
    value_type fetch_and_decrement() {
        return fetch_and_add<M>(__TBB_MINUS_ONE(D));
    }
...

See the correlation?

What you get is:

...
template<typename I, typename 2, typename StepType>
struct atomic_impl_with_arithmetic: atomic_impl<I> {
...
    value_type fetch_and_add( 2 addend ) {
...
    template<memory_semantics M>
    value_type fetch_and_decrement() {
        return fetch_and_add<M>(__TBB_MINUS_ONE(2));
    }
...

This is not valid C++.

Solution: Don't give macros a name that will probably clash. Rule of thumb: Every neat name clashes at least once in a lifetime and produce subtle and unreal error messages that might infect a poor maintainers (you?) nightly dreams with visions of indescribable demons. Therefore, only use macros if you absolutely must.

If D is supposed to be an integer (but who, except you, knows by that name?), you can do one of the following:

enum { D = 2 };

or

extern const int D; // in a header
const int D = 2; // in exactly one source file

or you may rethink your decision whether D needs to be constant at all and instead pass it as a parameter to your functions and classes.

OTHER TIPS

Here is an excerpt from tbb/atomic.h


template<typename I, typename D, typename StepType>
struct atomic_impl_with_arithmetic: atomic_impl<I> {
public:
    typedef I value_type;

    template<memory_semantics M>
    value_type fetch_and_add( D addend ) {
         return value_type(internal::atomic_traits<sizeof(value_type),M>::fetch_and_add( &this->rep.value, addend*sizeof(StepType) ));
    }
...

Now, if you substitute definition for D from Definitions.h you'll see what the issue is. In general it's a bad idea to make macro definitions with such names. If you've got too much code dependent on D you can fix the issue by undefining it befor including tbb headers, something like this:


#include "Definitions.h"
#include "Rk.h"

#pragma push_macro("D")
#undef D

#include "tbb/concurrent_hash_map.h"
#include <tbb/concurrent_vector.h>

#pragma pop_macro("D")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top