error: expected unqualified-id before ‘namespace’

T

tt

When I try to compile this file:

namespace test {

class Bob{

};

}

on Ubuntu I get:


error: expected unqualified-id before ‘namespace’. I have tried to strip
it down as much as possible, Any ideas?
 
A

Alf P. Steinbach

* tt:
When I try to compile this file:

namespace test {

class Bob{

};

}

on Ubuntu I get:


error: expected unqualified-id before ‘namespace’. I have tried to strip
it down as much as possible, Any ideas?

See the FAQ on how to post a question about code that doesn't work, then repost.


Cheers & hth.,

- Alf
 
V

Victor Bazarov

tt said:
When I try to compile this file:

namespace test {

class Bob{

};

}

on Ubuntu I get:


error: expected unqualified-id before ‘namespace’. I have tried to strip
^^^^^^
it down as much as possible, Any ideas?

Yes. Compile your code as C++.

V
 
T

tt

Victor said:
Yes. Compile your code as C++.

V


I run a Makefile generated with cmake. But if I just do:

g++ test.h -o test

where test.h is the file containing:

namespace test {

class Bob{

};

}

I get no errors. I am not sure what you mean with compiling the code as
C++, since that is what I have done.
 
N

Neelesh

I run a Makefile generated with cmake. But if I just do:

g++ test.h -o test

where test.h is the file containing:

namespace test {

    class Bob{

  };

  }

I get no errors.

Doesn't this indicate that there is some issue with the makefile
rather than with the C++ code?
 
V

Victor Bazarov

tt said:
I run a Makefile generated with cmake. But if I just do:

g++ test.h -o test

where test.h is the file containing:

namespace test {

class Bob{

};

}

I get no errors. I am not sure what you mean with compiling the code as
C++, since that is what I have done.

Yes, *now* I see that. And, *now* I see that you can't make up your
mind. In your original post you claim that "When I try to compile this
file" (with the same contents), you get errors. Here you say that "if I
just do" (compiling the same file), you "get no errors". So, which is
it? Do you get errors when you compile it, or don't you?

V
 
T

tt

Neelesh said:
Doesn't this indicate that there is some issue with the makefile
rather than with the C++ code?

Yes it does. This is the content from the Makefile that is generated
with cmake:





# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.6

# Default target executed when no arguments are given to make.
default_target: all
..PHONY : default_target

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canoncical targets will work.
..SUFFIXES:

# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =

..SUFFIXES: .hpux_make_needs_suffix_list

# Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
..PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E remove -f

# The program to use to edit the cache.
CMAKE_EDIT_COMMAND = /usr/bin/cmake-gui

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/tt/spec/RUS

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/tt/spec/RUS/build

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running
CMake cache editor..."
/usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
..PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
..PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running
CMake to regenerate build system..."
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
..PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
..PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start
/home/tt/spec/RUS/build/CMakeFiles
/home/tt/spec/RUS/build/CMakeFiles/progress.make
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start
/home/tt/spec/RUS/build/CMakeFiles 0
..PHONY : all

# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
..PHONY : clean

# The main clean target
clean/fast: clean
..PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
..PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
..PHONY : preinstall/fast

# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
--check-build-system CMakeFiles/Makefile.cmake 1
..PHONY : depend

#=============================================================================
# Target rules for targets named RUS

# Build rule for target.
RUS: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 RUS
..PHONY : RUS

# fast build rule for target.
RUS/fast:
$(MAKE) -f CMakeFiles/RUS.dir/build.make CMakeFiles/RUS.dir/build
..PHONY : RUS/fast

test.o: test.cpp.o
..PHONY : test.o

# target to build an object file
test.cpp.o:
$(MAKE) -f CMakeFiles/RUS.dir/build.make CMakeFiles/RUS.dir/test.cpp.o
..PHONY : test.cpp.o

test.i: test.cpp.i
..PHONY : test.i

# target to preprocess a source file
test.cpp.i:
$(MAKE) -f CMakeFiles/RUS.dir/build.make CMakeFiles/RUS.dir/test.cpp.i
..PHONY : test.cpp.i

test.s: test.cpp.s
..PHONY : test.s

# target to generate assembly for a file
test.cpp.s:
$(MAKE) -f CMakeFiles/RUS.dir/build.make CMakeFiles/RUS.dir/test.cpp.s
..PHONY : test.cpp.s

# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... RUS"
@echo "... edit_cache"
@echo "... rebuild_cache"
@echo "... test.o"
@echo "... test.i"
@echo "... test.s"
..PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
--check-build-system CMakeFiles/Makefile.cmake 0
..PHONY : cmake_check_build_system
 
M

Martijn Mulder

Yes it does. This is the content from the Makefile that is generated with
cmake:

# CMAKE generated file: DO NOT EDIT!



DO NOT use makefiles. They make your life miserable. With a little work you
can use scripts and response files that give you all the flexibility you
need. I don't know for Ubuntu, but on Windows (yes, Microsoft) I can simply
type

C:\cl.exe bob.cpp

and your program compiles without errors
 
T

tt

Martijn said:
DO NOT use makefiles. They make your life miserable. With a little work you
can use scripts and response files that give you all the flexibility you
need. I don't know for Ubuntu, but on Windows (yes, Microsoft) I can simply
type

C:\cl.exe bob.cpp

and your program compiles without errors

Thats not really an option I need to build it using cmake. I just don't
understand what it is in the Makefile that gives me the above error. If
I run the same file with:

g++ test.h -o test

or

c++ test.h -o test


there is no error. The Makefile generated with cmake is using c++ and if
I specify g++ it automatically changes to c++. But I don't think thats
the issue.
 
V

Victor Bazarov

tt said:
Thats not really an option I need to build it using cmake. I just don't
understand what it is in the Makefile that gives me the above error. If
I run the same file with:

g++ test.h -o test

or

c++ test.h -o test


there is no error. The Makefile generated with cmake is using c++ and if
I specify g++ it automatically changes to c++. But I don't think thats
the issue.

Do NOT compile *headers*. Compile your .C or .cc or .cpp or .cxx or
..whatever-extension-you're-using-for-the-file-containing-'main' files.

Do NOT post questions about 'make' (gmake, cmake, blahmake) here. We do
not discuss tools here. We discuss *language*.

V
 
J

joshuamaurice

DO NOT use makefiles. They make your life miserable. With a little work you
can use scripts and response files that give you all the flexibility you
need. I don't know for Ubuntu, but on Windows (yes, Microsoft) I can simply
type

Then you have obviously never worked on any project of a large size.
An incremental build is near required for code which takes hours to
compile. Also, a parallel build system is incredibly vital to make
those hours into (10s of) minutes.
 
Z

Zachary Turner

Thats not really an option I need to build it using cmake. I just don't
understand what it is in the Makefile that gives me the above error. If
I run the same file with:

g++ test.h -o test

or

c++ test.h -o test

there is no error. The Makefile generated with cmake is using c++ and if
I specify g++ it automatically changes to c++. But I don't think thats
the issue.

First of all the command you've just typed above is bogus. You
shouldn't compile .h files, you should compile .cpp files or .c
files. Assuming you have a file named test.cpp then you type

g++ test.cpp -o test

test.cpp would then have a line

#include "test.h"

at the top.

Secondly, assuming the problem really is with CMake and not because
you're trying to compile a header file, then I'm not sure anyone here
can help. I for one know nothing about CMake.
 
J

James Kanze

DO NOT use makefiles. They make your life miserable. With a
little work you can use scripts and response files that give
you all the flexibility you need. I don't know for Ubuntu, but
on Windows (yes, Microsoft) I can simply type
C:\cl.exe bob.cpp
and your program compiles without errors

I'd be very surprised. You have a very hacked installation if
cl is directly in the root directory of drive C:. If you've
installed the compiler correctly and set up your environment
like Microsoft recommends for command line invocation,
cl bob.cpp
should work, but that's only true for the simplest programs. In
practice, you need a number of additional options to get
anything usable. Something like:
cl -DNOMINMAX -D_CRT_SECURE_NO_DEPRECATE -vmg -GR -Gy -EHs -
Zc:forScope,wchar_t -J -MDd -GS- -Zi -w -D_DEBUG bob.cpp
So you'll want to put it into a batch file or a makefile---all
things considered, the make file is simpler, and does more.

I'm not familiar with cmake or nmake, but gmake---and all of the
usual Unix make's---support macros and generic rules, so you
don't have to write the command out for each file. A typical
gmake file for compiling and linking might be something as
simple as:
sources = bob.cpp
include $(makefileRoot)/binary.mk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top