2

An existing C++ multi project uses CMake 2.8 in QT so far. We want to continue maintaining it in Visual Studio 2010. I generated Visual Studio projects in CMake with option -G "Visual Studio 10" but now I fail to compile them for the following reason:

In the project we use a well known macro which itself is discussed for example in this question.

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);   \
  void operator=(const TypeName&)

The macro is defined in CMake in order to provide it to the compiler (cl.exe) as a preprocessor definition:

add_definitions(-DDISALLOW_COPY_AND_ASSIGN\(TypeName\)=\" TypeName(const TypeName&)\; void operator=(const TypeName&)\; \")

Visual Studio does not accept CMake's output and throws compilation errors wherever the macro is used in the code. What would be the proper syntax so that CMake can properly generate it for Visual Studio 2010?

Community
  • 1
  • 1
Finnfalter
  • 732
  • 2
  • 7
  • 22

2 Answers2

2

I would recommend that you don't use macros in general, and for this case in particular. If you can use boost, you can privately inherit from boost::noncopyable. If you don't, you can define your own:

class noncopyable {
   noncopyable(noncopyable const &);
   void operator=(noncopyable const&);
protected:
   noncopyable();
};

class Use : noncopyable
{
...

If you insist on using a macro, read the compiler documentation as of the flags that are needed to dump the preprocessed code into a file and see what the macro expanded to. From there you can try to figure out what went wrong.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Thanks a lot, David! I will stick with Your suggestion whenever I will set up a project myself (and vote You up when I have >14 credits). However, for now I go with Angew's answer because my VS project is only a branch from the QT project which itself is still under development and which I do not own. Using /FI allows me to compile VS minimally invasive. – Finnfalter Dec 10 '12 at 10:25
2

It's not possible to define function-style macros on the command line of cl. You could get around this by having the macro definition in a header file and passing this header file using cl's command-line option /FI. Or just include it manually where necessary (which is probably cleaner).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455