迁移至RHEL 9后Oracle Pro*C++编译#if __has_include报错排查
迁移至RHEL 9后老旧Pro*C++代码编译失败的解决方法
问题背景
将开发环境迁移到RHEL 9并升级新版GNU编译器后,老旧Pro*C++代码编译失败,报错核心涉及#if __has_include(<sys/single_threaded.h>)预处理指令。尝试谷歌搜索、Copilot辅助排查及Oracle官方支持均未找到有效方案,即使简化测试代码后仍出现异常语法错误。
复现信息
简化测试代码
#include <stdio.h> #if __has_include(<sys/single_threaded.h>) #include <sys/single_threaded.h> #endif EXEC SQL BEGIN DECLARE SECTION; int emp_id; EXEC SQL END DECLARE SECTION; int main() { EXEC SQL CONNECT :user IDENTIFIED BY :pass; printf("Test\n"); return 0; }
编译脚本
proc in=test.pc out=test.c code=cpp parse=full g++ -c test.c -o test.o
完整报错输出
Pro*C/C++: Release 19.0.0.0.0 - Production on Tue Aug 22 10:00:00 2023 Version 19.19.0.0.0 Copyright (c) 1982, 2023, Oracle and/or its affiliates. All rights reserved. System default option values taken from: /opt/oracle/product/19c/dbhome_1/precomp/admin/pcscfg.cfg test.pc:2: Error: PCC-S-02015, Encountered the symbol "__has_include" when expecting one of the following: ( begin case declare else elsif end exception for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted string> a bind variable << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge The symbol "if" was substituted for "__has_include" to continue. test.pc:2: Error: PCC-S-02007, Syntax error at or near line 2, column 6
排查思路
- 逐步简化代码后确认:只要包含
__has_include相关预处理指令,ProC预处理阶段就会报错,说明ProC的预处理器不支持GCC的__has_include扩展宏。 - 验证
ORA_PROC宏特性:ProC在预处理代码时会自动定义ORA_PROC宏,可用于区分ProC预处理和后续GCC编译阶段。
解决方案
用#ifndef ORA_PROC包裹包含__has_include的代码块,让Pro*C预处理阶段跳过不支持的代码,由后续GCC编译器处理:
#include <stdio.h> #ifndef ORA_PROC #if __has_include(<sys/single_threaded.h>) #include <sys/single_threaded.h> #endif #endif EXEC SQL BEGIN DECLARE SECTION; int emp_id; EXEC SQL END DECLARE SECTION; int main() { EXEC SQL CONNECT :user IDENTIFIED BY :pass; printf("Test\n"); return 0; }
重新执行编译脚本即可正常通过编译。
内容的提问来源于stack exchange,提问作者EvilTeach
相关产品推荐
相关产品推荐

