Ошибка macro names must be identifiers

I have the source code of an application written in C++ and I just want to comment something using:

#ifdef 0
...
#endif

And I get this error

error: macro names must be identifiers

Why is this happening?

Brian Tompsett - 汤莱恩's user avatar

asked Jan 9, 2009 at 1:27

Eduardo's user avatar

3

The #ifdef directive is used to check if a preprocessor symbol is defined. The standard (C11 6.4.2 Identifiers) mandates that identifiers must not start with a digit:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters>
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

The correct form for using the pre-processor to block out code is:

#if 0
: : :
#endif

You can also use:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif

but you need to be confident that the symbols will not be inadvertently set by code other than your own. In other words, don’t use something like NOTUSED or DONOTCOMPILE which others may also use. To be safe, the #if option should be preferred.

answered Jan 9, 2009 at 1:38

paxdiablo's user avatar

paxdiablopaxdiablo

849k233 gold badges1569 silver badges1944 bronze badges

3

Use the following to evaluate an expression (constant 0 evaluates to false).

#if 0
 ...
#endif

answered Jan 9, 2009 at 1:33

tvanfosson's user avatar

tvanfossontvanfosson

523k99 gold badges697 silver badges794 bronze badges

This error can also occur if you are not following the marco rules

Like

#define 1K 1024 // Macro rules must be identifiers error occurs

Reason: Macro Should begin with a letter, not a number

Change to

#define ONE_KILOBYTE 1024 // This resolves 

James Webster's user avatar

James Webster

31.8k11 gold badges69 silver badges114 bronze badges

answered Nov 9, 2011 at 5:37

AnotherDeveloper's user avatar

AnotherDeveloperAnotherDeveloper

2,1312 gold badges23 silver badges27 bronze badges

#ifdef 0
...
#endif

#ifdef expect a macro rather than expression
when using constant or expression

#if 0
...
#endif

or

#if !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

if #ifdef is used the it reports this error

#ifdef !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

Where #ifdef expect a macro rather than macro expresssion

Horizon_Net's user avatar

Horizon_Net

5,9594 gold badges31 silver badges34 bronze badges

answered May 28, 2014 at 13:28

Deepak Reddy's user avatar

Note that you can also hit this error if you accidentally type:

#define <stdio.h>

…instead of…

#include <stdio.>

answered Feb 2, 2013 at 23:00

Drew Noakes's user avatar

Drew NoakesDrew Noakes

299k164 gold badges677 silver badges739 bronze badges

Arduino Forum

Loading

11223344

0 / 0 / 0

Регистрация: 09.01.2014

Сообщений: 4

1

09.01.2014, 15:12. Показов 11008. Ответов 6

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Не знаю как исправить

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
main.cpp
#include <iostream>
#include "include/1.h"
 
int main()
{
 return 0;
}
 
1.h
#ifndef 1_H_INCLUDED
#define 1_H_INCLUDED
 
#endif // 1_H_INCLUDED
 
1.cpp
#include "../include/1.h"
 
int x = 1;
int b = 1;
 
include1.h|1|error: macro names must be identifiers

Добавлено через 8 минут
Подскажите



0



alsav22

5496 / 4891 / 831

Регистрация: 04.06.2011

Сообщений: 13,587

09.01.2014, 15:21

2

Если так:

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
main.cpp
#include <iostream>
#include "1.h"
 
int main()
{
   std::cout << x << ' ' << b << std::endl; 
 
   return 0;
}
 
1.h
#ifndef 1_H_INCLUDED
#define 1_H_INCLUDED
 extern int x;
 extern int b;
#endif // 1_H_INCLUDED
 
1.cpp
#include "1.h"
 
int x = 1;
int b = 1;



0



0 / 0 / 0

Регистрация: 09.01.2014

Сообщений: 4

09.01.2014, 15:25

 [ТС]

3

Тоже ошибка macro names must be identifiers



0



5496 / 4891 / 831

Регистрация: 04.06.2011

Сообщений: 13,587

09.01.2014, 15:33

4

Цитата
Сообщение от 11223344
Посмотреть сообщение

Тоже ошибка macro names must be identifiers

На какой строке?

Добавлено через 1 минуту
Коды по файлам разнесены?



0



0 / 0 / 0

Регистрация: 09.01.2014

Сообщений: 4

09.01.2014, 15:36

 [ТС]

5

На первой строчке в файле 1.h показывает

Добавлено через 1 минуту
Программа по разным файлам



0



alsav22

5496 / 4891 / 831

Регистрация: 04.06.2011

Сообщений: 13,587

09.01.2014, 15:37

6

Цифру перед макросом уберите:

C++
1
2
3
4
5
#ifndef H_INCLUDED
#define H_INCLUDED
 extern int x;
 extern int b;
#endif // H_INCLUDED



1



0 / 0 / 0

Регистрация: 09.01.2014

Сообщений: 4

09.01.2014, 15:43

 [ТС]

7

Спасибо alsav22. Теперь уже лучше и нет ошибок



0



New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account

Closed

duanyongli opened this issue

Aug 19, 2020

· 8 comments


· Fixed by #54

Comments

@duanyongli

platform: Jetson Xavier NX, ubuntu18.04
It seems like #9. But #9 had been resolved.

Screenshot from 2020-08-19 16-03-27

@schornakj

Can you copy and paste the full verbose output from catkin build? Offhand I believe the command for this is catkin build --verbose.

Some more info about the build environment will be helpful:

  • What version of PCL is installed?
  • What version of CUDA are you using?

@duanyongli

Thanks for your reply.

More info about the build environment:

  • What version of PCL is installed? PCL1.8
  • What version of CUDA are you using? cuda 10.2

verbose output follows:
[ 63%] Building CXX object CMakeFiles/yak_frontend.dir/src/yak_server.cpp.o
[ 68%] Building CXX object CMakeFiles/yak_frontend.dir/src/kfusion/tsdf_container.cpp.o
/usr/bin/c++ -D-ffloat-store -DDISABLE_DAVIDSDK -DDISABLE_DSSDK -DDISABLE_ENSENSO -DDISABLE_LIBUSB_1_0 -DDISABLE_PCAP -DDISABLE_PNG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DvtkFiltersFlowPaths_AUTOINIT=»1(vtkFiltersParallelFlowPaths)» -DvtkIOExodus_AUTOINIT=»1(vtkIOParallelExodus)» -DvtkIOGeometry_AUTOINIT=»1(vtkIOMPIParallel)» -DvtkIOImage_AUTOINIT=»1(vtkIOMPIImage)» -DvtkIOParallel_AUTOINIT=»1(vtkIOMPIParallel)» -DvtkIOSQL_AUTOINIT=»2(vtkIOMySQL,vtkIOPostgreSQL)» -DvtkRenderingContext2D_AUTOINIT=»1(vtkRenderingContextOpenGL)» -DvtkRenderingCore_AUTOINIT=»3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL)» -DvtkRenderingFreeType_AUTOINIT=»2(vtkRenderingFreeTypeFontConfig,vtkRenderingMatplotlib)» -DvtkRenderingLIC_AUTOINIT=»1(vtkRenderingParallelLIC)» -DvtkRenderingVolume_AUTOINIT=»1(vtkRenderingVolumeOpenGL)» -Dyak_frontend_EXPORTS -isystem /usr/include/vtk-6.3 -isystem /usr/include/freetype2 -isystem /usr/lib/aarch64-linux-gnu/openmpi/include/openmpi -isystem /usr/lib/aarch64-linux-gnu/openmpi/include/openmpi/opal/mca/event/libevent2022/libevent -isystem /usr/lib/aarch64-linux-gnu/openmpi/include/openmpi/opal/mca/event/libevent2022/libevent/include -isystem /usr/lib/aarch64-linux-gnu/openmpi/include -isystem /usr/include/python2.7 -isystem /usr/include/aarch64-linux-gnu -isystem /usr/include/hdf5/openmpi -isystem /usr/include/libxml2 -isystem /usr/include/jsoncpp -isystem /usr/include/tcl -I/usr/local/cuda/include -I/home/research/catkin_ws_test/src/yak/yak/include -isystem /usr/include/pcl-1.8 -isystem /usr/include/eigen3 -isystem /usr/include/ni -isystem /usr/include/openni2 -isystem /usr/include/opencv -isystem /usr/include/aarch64-linux-gnu/qt5 -isystem /usr/include/aarch64-linux-gnu/qt5/QtWidgets -isystem /usr/include/aarch64-linux-gnu/qt5/QtGui -isystem /usr/include/aarch64-linux-gnu/qt5/QtCore -isystem /usr/lib/aarch64-linux-gnu/qt5/mkspecs/linux-g++ -fPIC -fPIC -std=gnu++14 -o CMakeFiles/yak_frontend.dir/src/yak_server.cpp.o -c /home/research/catkin_ws_test/src/yak/yak/src/yak_server.cpp
/usr/bin/c++ -D-ffloat-store -DDISABLE_DAVIDSDK -DDISABLE_DSSDK -DDISABLE_ENSENSO -DDISABLE_LIBUSB_1_0 -DDISABLE_PCAP -DDISABLE_PNG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DvtkFiltersFlowPaths_AUTOINIT=»1(vtkFiltersParallelFlowPaths)» -DvtkIOExodus_AUTOINIT=»1(vtkIOParallelExodus)» -DvtkIOGeometry_AUTOINIT=»1(vtkIOMPIParallel)» -DvtkIOImage_AUTOINIT=»1(vtkIOMPIImage)» -DvtkIOParallel_AUTOINIT=»1(vtkIOMPIParallel)» -DvtkIOSQL_AUTOINIT=»2(vtkIOMySQL,vtkIOPostgreSQL)» -DvtkRenderingContext2D_AUTOINIT=»1(vtkRenderingContextOpenGL)» -DvtkRenderingCore_AUTOINIT=»3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL)» -DvtkRenderingFreeType_AUTOINIT=»2(vtkRenderingFreeTypeFontConfig,vtkRenderingMatplotlib)» -DvtkRenderingLIC_AUTOINIT=»1(vtkRenderingParallelLIC)» -DvtkRenderingVolume_AUTOINIT=»1(vtkRenderingVolumeOpenGL)» -Dyak_frontend_EXPORTS -isystem /usr/include/vtk-6.3 -isystem /usr/include/freetype2 -isystem /usr/lib/aarch64-linux-gnu/openmpi/include/openmpi -isystem /usr/lib/aarch64-linux-gnu/openmpi/include/openmpi/opal/mca/event/libevent2022/libevent -isystem /usr/lib/aarch64-linux-gnu/openmpi/include/openmpi/opal/mca/event/libevent2022/libevent/include -isystem /usr/lib/aarch64-linux-gnu/openmpi/include -isystem /usr/include/python2.7 -isystem /usr/include/aarch64-linux-gnu -isystem /usr/include/hdf5/openmpi -isystem /usr/include/libxml2 -isystem /usr/include/jsoncpp -isystem /usr/include/tcl -I/usr/local/cuda/include -I/home/research/catkin_ws_test/src/yak/yak/include -isystem /usr/include/pcl-1.8 -isystem /usr/include/eigen3 -isystem /usr/include/ni -isystem /usr/include/openni2 -isystem /usr/include/opencv -isystem /usr/include/aarch64-linux-gnu/qt5 -isystem /usr/include/aarch64-linux-gnu/qt5/QtWidgets -isystem /usr/include/aarch64-linux-gnu/qt5/QtGui -isystem /usr/include/aarch64-linux-gnu/qt5/QtCore -isystem /usr/lib/aarch64-linux-gnu/qt5/mkspecs/linux-g++ -fPIC -fPIC -std=gnu++14 -o CMakeFiles/yak_frontend.dir/src/kfusion/tsdf_container.cpp.o -c /home/research/catkin_ws_test/src/yak/yak/src/kfusion/tsdf_container.cpp
[ 84%] Built target yak_marching_cubes
/usr/bin/make -f CMakeFiles/marching_cubes_tests.dir/build.make CMakeFiles/marching_cubes_tests.dir/depend
make[2]: Entering directory ‘/home/research/catkin_ws_test/build/yak’
cd /home/research/catkin_ws_test/build/yak && /usr/bin/cmake -E cmake_depends «Unix Makefiles» /home/research/catkin_ws_test/src/yak/yak /home/research/catkin_ws_test/src/yak/yak /home/research/catkin_ws_test/build/yak /home/research/catkin_ws_test/build/yak /home/research/catkin_ws_test/build/yak/CMakeFiles/marching_cubes_tests.dir/DependInfo.cmake —color=
:0:1: error: macro names must be identifiers
:0:1: error: macro names must be identifiers
make[2]: Leaving directory ‘/home/research/catkin_ws_test/build/yak’
/usr/bin/make -f CMakeFiles/marching_cubes_tests.dir/build.make CMakeFiles/marching_cubes_tests.dir/build
make[2]: Entering directory ‘/home/research/catkin_ws_test/build/yak’
make[2]: Nothing to be done for ‘CMakeFiles/marching_cubes_tests.dir/build’.
make[2]: Leaving directory ‘/home/research/catkin_ws_test/build/yak’
[ 94%] Built target marching_cubes_tests
CMakeFiles/yak_frontend.dir/build.make:86: recipe for target ‘CMakeFiles/yak_frontend.dir/src/kfusion/tsdf_container.cpp.o’ failed
make[2]: *** [CMakeFiles/yak_frontend.dir/src/kfusion/tsdf_container.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs….
CMakeFiles/yak_frontend.dir/build.make:62: recipe for target ‘CMakeFiles/yak_frontend.dir/src/yak_server.cpp.o’ failed
make[2]: Leaving directory ‘/home/research/catkin_ws_test/build/yak’
make[2]: *** [CMakeFiles/yak_frontend.dir/src/yak_server.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/yak_frontend.dir/all’ failed
make[1]: Leaving directory ‘/home/research/catkin_ws_test/build/yak’
make[1]: *** [CMakeFiles/yak_frontend.dir/all] Error 2
Makefile:129: recipe for target ‘all’ failed
make: *** [all] Error 2
cd /home/research/catkin_ws_test/build/yak; catkin build —get-env yak | catkin env -si /usr/bin/make —jobserver-fds=6,7 -j; cd —

@schornakj

@duanyongli Could you select the text in your console and paste it as a comment instead of posting it as a screenshot? It’s easier to read that way, and we can search it for keywords too.

@schornakj

In any case, I’m suspicious of that -D-ffloat-store argument at the beginning of the list of compiler arguments. -ffloat-store is a predefined GCC flag, and prepending -D to something tells GCC to treat whatever comes after -D as a macro name, which isn’t the right thing to do in this case. I think the error you see is because macro names can’t have hyphens in them, so the compiler gives an error when it encounters the invalid name.

I don’t think that this is a problem with the Yak library or its dependencies, since we have CI jobs that build with CUDA 10.2 on Ubuntu 18.04. If you can find what is adding -D-ffloat-store to the compiler options and figure out how to remove it I think you’ll be able to solve this problem.

@duanyongli

In any case, I’m suspicious of that -D-ffloat-store argument at the beginning of the list of compiler arguments. -ffloat-store is a predefined GCC flag, and prepending -D to something tells GCC to treat whatever comes after -D as a macro name, which isn’t the right thing to do in this case. I think the error you see is because macro names can’t have hyphens in them, so the compiler gives an error when it encounters the invalid name.

I don’t think that this is a problem with the Yak library or its dependencies, since we have CI jobs that build with CUDA 10.2 on Ubuntu 18.04. If you can find what is adding -D-ffloat-store to the compiler options and figure out how to remove it I think you’ll be able to solve this problem.

I think -D-ffloat-store comes from PCL.

find_package(PCL 1.8 REQUIRED COMPONENTS common io geometry)
message(WARNING ${PCL_DEFINITIONS})

message says:
CMake Warning at CMakeLists.txt:17 (message):
-ffloat-store
-DDISABLE_ENSENSO-DDISABLE_DAVIDSDK-DDISABLE_DSSDK-DDISABLE_PCAP-DDISABLE_PNG-DDISABLE_LIBUSB_1_0

@schornakj

Based on some quick research it seems likely that -ffloat-store is automatically added by PCL based on the detected capabilities of the ARM CPU in the Jetson Xavier NX.

message says:
CMake Warning at CMakeLists.txt:17 (message):
-ffloat-store
-DDISABLE_ENSENSO-DDISABLE_DAVIDSDK-DDISABLE_DSSDK-DDISABLE_PCAP-DDISABLE_PNG-DDISABLE_LIBUSB_1_0

It’s significant that the -ffloat-store flag is written correctly here. That means that some later step is incorrectly adding the -D prefix.

I don’t think we’ve seen a -f flag in PCL_DEFINITIONS before, so this might be a case that Yak’s CMakeLists.txt doesn’t handle correctly. For PCL 1.9 the contents of PCL_DEFINITIONS get passed into target_compile_definitions for the yak_frontend library at this line, and that might not be the correct way to handle flags like -ffloat-store. We have a macro to parse mixed options and definitions, so we might need to create something similar to separate mixed flags and definitions.

@schornakj

@duanyongli I was able to replicate this by manually-prepending -ffloat-store to PCL_DEFINITIONS. Our target_add_options_and_definitions macro doesn’t check to see if the prefix is -f, so -ffloat-store was incorrectly being added through target_compile_definitions instead of target_compile_options. There were also some issues with PCL_DEFINITIONS being inconsistently-delimited so that multiple flags and definitions would be incorrectly concatenated. As discussed in #9 and #10 this is specifically a problem with PCL 1.8.X and it’s been fixed in PCL 1.9.X and later.

I modified the macro to correctly handle -f-prefixed options and inconsistent delimiters, and I’ll have a PR with the fix submitted shortly.

@schornakj

@duanyongli Sorry this took a long time to get fixed and merged in. Let us know if you still encounter this problem on the newest version.

2 participants

@duanyongli

@schornakj

У меня есть исходный код приложения, написанного на С++, и я просто хочу что-то прокомментировать, используя:

#ifdef 0
...
#endif

И я получаю эту ошибку

ошибка: имена макросов должны быть идентификаторами

Почему это происходит?

4b9b3361

Ответ 1

Директива #ifdef используется для проверки наличия символа препроцессора. Стандарт (C11 6.4.2 Identifiers) требует, чтобы идентификаторы не начинались с цифры:

identifier:
    identifier-nondigit
    identifier identifier-nondigit
    identifier digit
identifier-nondigit:
    nondigit
    universal-character-name
    other implementation-defined characters>
nondigit: one of
    _ a b c d e f g h i j k l m
    n o p q r s t u v w x y z
    A B C D E F G H I J K L M
    N O P Q R S T U V W X Y Z
digit: one of
    0 1 2 3 4 5 6 7 8 9

Правильная форма использования препроцессора для блокировки кода:

#if 0
: : :
#endif

Вы также можете использовать:

#ifdef NO_CHANCE_THAT_THIS_SYMBOL_WILL_EVER_EXIST
: : :
#endif

но вам нужно быть уверенным, что символы не будут непреднамеренно установлены кодом, отличным от вашего. Другими словами, не используйте что-то вроде NOTUSED или DONOTCOMPILE, которое другие могут использовать. Чтобы быть в безопасности, предпочтительнее выбрать #if.

Ответ 2

Используйте следующее, чтобы оценить выражение (константа 0 принимает значение false).

#if 0
 ...
#endif

Ответ 3

Эта ошибка также может возникать, если вы не выполняете правила marco

Как

#define 1K 1024 // Macro rules must be identifiers error occurs

Причина: макрос Должен начинаться с буквы, а не числа

Изменить на

#define ONE_KILOBYTE 1024 // This resolves 

Ответ 4

Обратите внимание, что вы также можете нажать эту ошибку, если случайно введите:

#define <stdio.h>

… вместо…

#include <stdio.>

Ответ 5

#ifdef 0
...
#endif

#ifdef ожидает макрос, а не выражение
при использовании константы или выражения

#if 0
...
#endif

или

#if !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

если используется #ifdef, он сообщает об этой ошибке

#ifdef !defined(PP_CHECK) || defined(PP_CHECK_OTHER)
..
#endif

Где #ifdef ожидает макрос, а не выражение макроса

Ответ 6

Иногда, если вы копируете код из PDF-документа и вставляете его в свой редактор; скрытые странные символы включены. Таким образом, эта ошибка может также возникать в этой ситуации.

Будьте осторожны. Вы должны заменить все пробелы, скопированные для новых, а также сделать то же самое с возвратом каретки.

Понравилась статья? Поделить с друзьями:
  • Ошибка man edc 03776 01
  • Ошибка machine settings file already exists
  • Ошибка man edc 00609 08
  • Ошибка mach3 no driver sensed installed
  • Ошибка makeinlineautocalloriginal originalptr is 0