gcc expected initializer before ‘class’

本文分享了一种解决难以捉摸的C/C++编译错误的方法,通过使用GCC的-E选项输出预处理结果,帮助定位头文件中可能存在的问题。

今天写代码的时候遇到了这个错误,想跟大家分享下。

这是一个神奇的编译错误。报错的地方打开可能完全看不到哪里有问题,甚至你上网找也不好找到具体出错的原因。

看报错内容,应该是class前缺少一些东西导致他认为出错了,比如函数定义最后少了一个分号之类的。但是打开出错的文件一看,可能就一个include,然后就是class了,根本看不出少了什么东西。这里,了解c编译流程的同学就知道了,include头文件,实际上在预处理阶段,就是把文件直接展开,然后根据宏进行各种替换而已。所以很有可能,是头文件里面,哪里出错了,然后在这个文件里include,被展开,之后编译报错。

此时怎么办呢?难道一个个头文件翻过去?如果你一边写一遍编译,那就翻一下记录就好。但是如果不是呢?怎么办?甚至这个可能是一个开源项目,别人写的,你不熟,怎么办?要知道,include进来的头文件,可能也include了头文件,没有人保证错误就是出在直接include的那个文件。比如今天我遇到的错误,就是在好几层外面include进来的文件有一个错误导致了问题。

由于这个错误是编译错误,不是链接错误,所以实际上我们只要知道经过预处理的文件怎么样,就知道怎么处理了。gcc输出预处理结果的选项为-E

这个时候,你只要把原本用来编译这个文件的gcc命令,一般这个阶段,如果使用的是makefile,输出的是-o x.o 目标文件,只需要把这个-o x.o替换成-E,然后重定向到一个文件即可。

比如原本编译命令为:

gcc a.cpp -I./ -o a.o

这里把当前目录加入到头文件的搜索路径,然后编译a.cpp,输出a.o,obj文件。

我们只需要用

gcc a.cpp -I./ -E > output.cpp

即可得到预编译之后的结果。然后就能很直观的看到,具体哪里出错了。

一般来说,常见的,影响预编译的选项包括:

  1. -I./     头文件搜索路径
  2.  -DNUM=1 宏定义
  3. -std=c++11 c标准定义
  4. mac的话可能会有-f frameword之类的选项,导入框架
  5. 内核或者嵌入式,或者交叉编译之类的,会有-nostdinc 或者 -nostdinc++ ,禁止本地标准头文件引入(需要使用目标平台的头文件)。
  6. 还有一些头文件搜索路径的环境变量,比如C_INCLUDE_PATH和CPLUS_INCLUDE_PATH等。

当然还有各种Error选项和Warning选项。非常非常特殊的项目,还会有取消宏定义之类的参数,这里不细究,一般就是这些了。所以使用-E参数的时候要保证这些内容跟make的时候一样,即可拉。

解析以下错误libs using C++ compiler: ‘g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)’ using C++11 g++ -std=gnu++11 -I"/public/software/R4.3.2/lib64/R/include" -DNDEBUG -I'/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/BH/include' -I'/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include' -I/usr/local/include -fpic -g -O2 -c cpp11.cpp -o cpp11.o In file included from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11.hpp:3:0, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/declarations.hpp:13, from cpp11.cpp:5: /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/R.hpp:52:20: error: missing space between ‘""’ and suffix identifier constexpr R_xlen_t operator""_xl(unsigned long long int value) { return value; } ^ In file included from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp:19:0, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/list.hpp:9, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/data_frame.hpp:12, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11.hpp:7, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/declarations.hpp:13, from cpp11.cpp:5: /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/named_arg.hpp:42:18: error: missing space between ‘""’ and suffix identifier inline named_arg operator""_nm(const char* name, std::size_t) { return named_arg(name); } ^ In file included from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/list.hpp:9:0, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/data_frame.hpp:12, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11.hpp:7, from /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/declarations.hpp:13, from cpp11.cpp:5: /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp: In member function ‘cpp11::r_vector<T>::const_iterator& cpp11::r_vector<T>::const_iterator::operator--()’: /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp:680:23: error: unable to find numeric literal operator ‘operator"" _xl’ fill_buf(std::max(0_xl, pos_ - 64)); ^ /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp: In member function ‘cpp11::r_vector<T>::const_iterator& cpp11::r_vector<T>::const_iterator::operator-=(R_xlen_t)’: /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp:700:23: error: unable to find numeric literal operator ‘operator"" _xl’ fill_buf(std::max(0_xl, pos_ - 64)); ^ /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp: In member function ‘void cpp11::r_vector<T>::const_iterator::fill_buf(R_xlen_t)’: /public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/cpp11/include/cpp11/r_vector.hpp:761:22: error: unable to find numeric literal operator ‘operator"" _xl’ length_ = std::min(64_xl, data_->size() - pos); ^ make: *** [cpp11.o] Error 1 ERROR: compilation failed for package ‘BiocParallel’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/BiocParallel’ * installing *source* package ‘Rcpp’ ... ** package ‘Rcpp’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)’ g++ -std=gnu++11 -I"/public/software/R4.3.2/lib64/R/include" -DNDEBUG -I../inst/include/ -I/usr/local/include -fpic -g -O2 -c api.cpp -o api.o In file included from ../inst/include/Rcpp/Module.h:510:0, from ../inst/include/Rcpp.h:70, from api.cpp:26: ../inst/include/Rcpp/module/class.h:114:100: error: expected ‘)’ before ‘(’ token self& constructor( const char* docstring = 0, ValidConstructor valid = &yes_arity<sizeof...(T)> ){ ^ ../inst/include/Rcpp/module/class.h:114:103: error: expected initializer before ‘>’ token self& constructor( const char* docstring = 0, ValidConstructor valid = &yes_arity<sizeof...(T)> ){ ^ ../inst/include/Rcpp/module/class.h:119:117: error: expected ‘)’ before ‘(’ token self& factory( Class* (*fun)(T...), const char* docstring = 0, ValidConstructor valid = &yes_arity<sizeof...(T)> ){ ^ ../inst/include/Rcpp/module/class.h:119:120: error: expected initializer before ‘>’ token self& factory( Class* (*fun)(T...), const char* docstring = 0, ValidConstructor valid = &yes_arity<sizeof...(T)> ){ ^ ../inst/include/Rcpp/module/class.h:265:89: error: expected ‘)’ before ‘(’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:265:92: error: expected initializer before ‘>’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:271:89: error: expected ‘)’ before ‘(’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:271:92: error: expected initializer before ‘>’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:277:98: error: expected ‘)’ before ‘(’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:277:101: error: expected initializer before ‘>’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:283:95: error: expected ‘)’ before ‘(’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:283:98: error: expected initializer before ‘>’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:289:89: error: expected ‘)’ before ‘(’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:289:92: error: expected initializer before ‘>’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:295:95: error: expected ‘)’ before ‘(’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h:295:98: error: expected initializer before ‘>’ token const char* docstring = 0, ValidMethod valid = &yes_arity<sizeof...(T)>) { ^ ../inst/include/Rcpp/module/class.h: In member function ‘Rcpp::class_<Class>::self& Rcpp::class_<Class>::default_constructor(const char*, Rcpp::ValidConstructor)’: ../inst/include/Rcpp/module/class.h:110:50: error: there are no arguments to ‘constructor’ that depend on a template parameter, so a declaration of ‘constructor’ must be available [-fpermissive] return constructor( docstring, valid ) ; ^ ../inst/include/Rcpp/module/class.h:110:50: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated) make: *** [api.o] Error 1 ERROR: compilation failed for package ‘Rcpp’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/Rcpp’ ERROR: dependency ‘Rcpp’ is not available for package ‘plyr’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/plyr’ ERROR: dependency ‘BiocParallel’ is not available for package ‘Rsamtools’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/Rsamtools’ ERROR: dependencies ‘plyr’, ‘Rcpp’ are not available for package ‘reshape2’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/reshape2’ ERROR: dependencies ‘Rsamtools’, ‘BiocParallel’ are not available for package ‘GenomicAlignments’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/GenomicAlignments’ ERROR: dependency ‘plyr’ is not available for package ‘emdbook’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/emdbook’ ERROR: dependency ‘reshape2’ is not available for package ‘qvalue’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/qvalue’ ERROR: dependencies ‘Rsamtools’, ‘GenomicAlignments’ are not available for package ‘rtracklayer’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/rtracklayer’ ERROR: dependencies ‘qvalue’, ‘emdbook’, ‘Rsamtools’, ‘rtracklayer’, ‘Rcpp’ are not available for package ‘methylKit’ * removing ‘/public/home/ouyanggroup/zhuyt/R/x86_64-pc-linux-gnu-library/4.3/methylKit’
10-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值