error: macro "min" passed 3 arguments, but takes just 2 error: expected ‘)’ before ‘const’ 解决方法

本文解决了在Ubuntu下使用g++编译C++程序时遇到的宏定义冲突问题,通过取消定义max和min宏,避免了与STL中同名宏冲突导致的编译错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在ubuntu下编译一个c++的solution,好几个project都得到如下的奇怪错误:


In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/istream:40,
                 from /share1/linux_1579/include/ospace/std/istream:13,
                 from /share1/linux_1579/include/ospace/osstd/iterator.h:20,
                 from /share1/linux_1579/include/ospace/osstd/list.h:16,
                 from /share1/linux_1579/include/ospace/std/list:15,
                 from ../../include/LrwVUserVars.h:70,
                 from ../../include/LrwLogicMgrUtil.h:19,
                 from LrwLogicMgrUtil.cpp:6:

/usr/include/c++/4.6/bits/stl_algobase.h:233:56: error: macro "min" passed 3 arguments, but takes just 2

/usr/include/c++/4.6/bits/stl_algobase.h:254:56: error: macro "max" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/istream:40,
                 from /share1/linux_1579/include/ospace/std/istream:13,
                 from /share1/linux_1579/include/ospace/osstd/iterator.h:20,
                 from /share1/linux_1579/include/ospace/osstd/list.h:16,
                 from /share1/linux_1579/include/ospace/std/list:15,
                 from ../../include/LrwVUserVars.h:70,
                 from ../../include/LrwLogicMgrUtil.h:19,
                 from LrwLogicMgrUtil.cpp:6:
/usr/include/c++/4.6/bits/stl_algobase.h:187:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:187:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:187:5: error: expected initializer before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: error: expected initializer before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:233:5: error: ‘std::min’ declared as an ‘inline’ variable
/usr/include/c++/4.6/bits/stl_algobase.h:233:5: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.6/bits/stl_algobase.h:236:7: error: expected primary-expression before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:236:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:238:7: error: expected unqualified-id before ‘return’
/usr/include/c++/4.6/bits/stl_algobase.h:254:5: error: ‘max’ declared as an ‘inline’ variable
/usr/include/c++/4.6/bits/stl_algobase.h:254:5: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.6/bits/stl_algobase.h:257:7: error: expected primary-expression before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:257:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:259:7: error: expected unqualified-id before ‘return’

/usr/include/c++/4.6/bits/stl_algobase.h:260:5: error: expected declaration before ‘}’ token


花了很多时间都没有找到解决方法。终于在网上找到一点线索,然后一点一点试找到了一个workaround。其实这个错误与代码本身或g++版本无关,只是因为出现了宏定义冲突:

所以只需把属于自己定义的相关头文件(比如上面的LrwVUserVars.h)中加入(最好加在自己的头文件引用和标准头文件的引用之间):

#include "LrwDomBase.h"
#include "LrwPostId.h"

//added by andy

#undef max
#undef min


#include <map>
#include <queue>

则这个问题就不会再出现了。


brising.c:14:10: error: conflicting types for ‘p’; have ‘Particle’ /home/zhoushizhe/basilisk/src/navier-stokes/centered.h:44:9: note: previous definition of ‘p’ with type ‘scalar’ brising.c:15:9: error: redefinition of ‘cs’ /home/zhoushizhe/basilisk/src/embed.h:19:9: note: previous definition of ‘cs’ with type ‘scalar’ brising.c:16:9: error: redefinition of ‘mu’ /home/zhoushizhe/basilisk/src/navier-stokes/centered.h:70:16: note: previous definition of ‘mu’ with type ‘vector’ brising.c: In function ‘main’: brising.c:21:5: error: ‘H0’ undeclared (first use in this function); did you mean ‘L0’? brising.c:21:5: note: each undeclared identifier is reported only once for each function it appears in /home/zhoushizhe/basilisk/src/grid/config.h:70:28: error: request for member ‘i’ in something not a structure or union /home/zhoushizhe/basilisk/src/grid/config.h:71:22: note: in expansion of macro ‘is_constant’ brising.c:23:10: note: in expansion of macroconstant’ /home/zhoushizhe/basilisk/src/grid/config.h:71:52: error: request for member ‘i’ in something not a structure or union brising.c:23:10: note: in expansion of macroconstant’ brising.c: In function ‘update_solid_fraction’: brising.c:42:5: error: too few arguments to function ‘embed_geometry’ /home/zhoushizhe/basilisk/src/embed.h:223:8: note: declared here /home/zhoushizhe/basilisk/src/grid/variables.h: In function ‘init_0’: /home/zhoushizhe/basilisk/src/grid/variables.h:3:29: error: ‘point’ undeclared (first use in this function); did you mean ‘Point’? /home/zhoushizhe/basilisk/src/embed.h:733:16: error: ‘data’ undeclared (first use in this function); did you mean ‘dt’? /home/zhoushizhe/basilisk/src/embed.h:733:60: warning: left-hand operand of comma expression has no effect [-Wunused-value] /home/zhoushizhe/basilisk/src/embed.h:734:28: warning: left-hand operand of comma expression has no effect [-Wunused-value] /home/zhoushizhe/basilisk/src/embed.h:734:48: error: ‘_s’ undeclared (first use in this function); did you mean ‘cs’? /home/zhoushizhe/basilisk/src/grid/config.h:538:22: note: in definition of macro ‘_index’ /home/zhoushizhe/basilisk/src/embed.h:734:44: note: in expansion of macro ‘val’ brising.c:55:2: error: subscripted value is neither array nor pointer nor vector brising.c:58:11: error: incompatible types when assigning to type ‘scalar’ from type ‘double’ brising.c: In function ‘axi_embed_force’: brising.c:75:48: error: incompatible type for argument 2 of ‘_stencil_embed_interpolate’ /home/zhoushizhe/basilisk/src/embed.h:255:61: note: expected ‘scalar’ but argument is of type ‘Particle’ brising.c:75:52: error: incompatible type for argument 2 of ‘embed_interpolate’ /home/zhoushizhe/basilisk/src/embed.h:255:47: note: expected ‘scalar’ but argument is of type ‘Particle’ brising.c: In function ‘compute_forces’: brising.c:102:15: error: invalid initializer brising.c:103:33: error: invalid operands to binary - (have ‘scalar’ and ‘double’) ast/init_solver.h: In function ‘_init_solver’: ast/init_solver.h:23:14: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:38: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:25: warning: left-hand operand of comma expression has no effect [-Wunused-value] ast/init_solver.h:23:81: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:68: warning: left-hand operand of comma expression has no effect [-Wunused-value] ast/init_solver.h:23:148: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:172: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:159: warning: left-hand operand of comma expression has no effect [-Wunused-value] ast/init_solver.h:23:214: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:201: warning: left-hand operand of comma expression has no effect [-Wunused-value] ast/init_solver.h:23:558: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:582: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:569: warning: left-hand operand of comma expression has no effect [-Wunused-value] ast/init_solver.h:23:623: error: ‘Particle’ has no member named ‘i’ ast/init_solver.h:23:610: warning: left-hand operand of comma expression has no effect [-Wunused-value]
06-05
我现在安装了gcc-12,执行make后报错:$ make make -C /lib/modules/6.8.0-65-generic/build M=/home/tp/kernel_code modules make[1]: 进入目录“/usr/src/linux-headers-6.8.0-65-generic” warning: the compiler differs from the one used to build the kernel The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0 You are using: gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0 CC [M] /home/tp/kernel_code/char_dev.o In file included from ./arch/x86/include/asm/mem_encrypt.h:15, from ./include/linux/mem_encrypt.h:17, from ./arch/x86/include/asm/processor-flags.h:6, from ./arch/x86/include/asm/processor.h:5, from ./arch/x86/include/asm/timex.h:5, from ./include/linux/timex.h:67, from ./include/linux/time32.h:13, from ./include/linux/time.h:60, from ./include/linux/stat.h:19, from ./include/linux/module.h:13, from /home/tp/kernel_code/char_dev.c:1: /home/tp/kernel_code/char_dev.c: In function ‘char_dev_init’: ./include/linux/init.h:188:22: error: passing argument 1 of ‘class_create’ from incompatible pointer type [-Werror=incompatible-pointer-types] 188 | #define THIS_MODULE (&__this_module) | ~^~~~~~~~~~~~~~~ | | | struct module * /home/tp/kernel_code/char_dev.c:83:31: note: in expansion of macro ‘THIS_MODULE’ 83 | char_class = class_create(THIS_MODULE, CLASS_NAME); | ^~~~~~~~~~~ In file included from ./include/linux/device.h:31, from ./include/linux/cdev.h:8, from /home/tp/kernel_code/char_dev.c:3: ./include/linux/device/class.h:228:54: note: expectedconst char *’ but argument is of type ‘struct module *’ 228 | class * __must_check class_create(const char *name); | ~~~~~~~~~~^~ /home/tp/kernel_code/char_dev.c:83:18: error: too many arguments to function ‘class_create’ 83 | char_class = class_create(THIS_MODULE, CLASS_NAME); | ^~~~~~~~~~~~ ./include/linux/device/class.h:228:29: note: declared here 228 | struct class * __must_check class_create(const char *name); | ^~~~~~~~~~~~ cc1: some warnings being treated as errors make[3]: *** [scripts/Makefile.build:243:/home/tp/kernel_code/char_dev.o] 错误 1 make[2]: *** [/usr/src/linux-headers-6.8.0-65-generic/Makefile:1925:/home/tp/kernel_code] 错误 2 make[1]: *** [Makefile:240:__sub-make] 错误 2 make[1]: 离开目录“/usr/src/linux-headers-6.8.0-65-generic” make: *** [Makefile:5:all] 错误 2
最新发布
08-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值