g++ 编译 cpp 过程中遇到问题

博客介绍了g++编译支持C++11的相关内容。指出g++ 4.7才真正支持C++11,给出了g++编译时支持C++11的命令,如“g++ -std=c++11 -pthread testserver.cpp -o testserver”,还提到CLion编译报错时需修改CMakeLists.txt,以及两种支持C++11的方法。

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

$ ./testserver
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
已放弃
[shinow@localhost test]$ g++ testserver.cpp -o testserver
[shinow@localhost test]$ ./testserver
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
已放弃
[shinow@localhost test]$ g++ -std=c++11 -pthread testserver.cpp -o testserver

如果clion编译时,会报这种错误。需要在CMakeLists.txt修改编译选项。

 

g++  4.7 才真正支持 c++11

g++编译时支持C++11

 

  1. //第一种,直接包含在源程序文件中,如第一行代码所示  
  2. #pragma GCC diagnostic error "-std=c++11"  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main(int argc,char **argv)  
  6. {  
  7.     cout<<"hello world!"<<endl;  
  8.     auto i=10;  
  9.     cout<<i<<endl;  
  10.     return 0;  
  11. }  
  12.   
  13.   
  14. //第二种方法,可以直接取别名方法 如下所示:  
  15. alias g++='g++ -std=c++11'  
  16.   
  17. g++ hw.cpp -o hw  
<think>好的,用户的问题是在Ubuntu上使用g++编译与Oracle相关的C++文件。我需要先整理已有的引用内容,看看哪些信息可以利用。 首先,用户提供的引用中,引用[3]提到编译机器不一定需要安装Oracle,只需要拷贝BIN和OCI文件夹。这说明用户需要配置Oracle客户端库,而不是完整的Oracle安装。引用[1]提到了OCILIB,这是一个开源的C/C++库,可能可以作为替代方案。引用[5]涉及Qt编译Oracle驱动的步骤,其中提到了Oracle客户端SDK的下载和解压,这可能和配置OCI有关。 接下来,我需要考虑如何在Ubuntu上配置Oracle Instant Client,因为这是编译OCI程序的前提。用户需要使用g++,所以需要安装开发包,比如instantclient-basic和instantclient-sdk。然后设置环境变量,如LD_LIBRARY_PATH指向库路径,并在编译命令中指定头文件路径和链接库。 另外,引用[2]中提到了交叉编译工具链的设置,虽然这里可能不需要交叉编译,但设置编译器的路径和环境变量的方法可以参考。比如在编译时使用-I指定头文件目录,-L指定库路径,-l链接oci库。 还需要注意动态库的运行时路径,可以通过ldconfig或者设置LD_LIBRARY_PATH来解决。可能用户会遇到链接错误,比如不到oci.h或者库文件,这时候需要检查路径是否正确。 可能还需要示例代码来演示如何连接Oracle数据库,并编译运行。这可以帮助用户验证他们的配置是否正确。 最后,生成相关问题,比如OCI和OCILIB的区别,解决编译时的常见错误,动态库加载问题,以及交叉编译的可能性。这些问题可以帮助用户进一步理解相关的内容。</think>### 如何在Ubuntu上使用g++编译Oracle相关C++文件 #### 1. 安装Oracle Instant Client - **下载并解压Oracle Instant Client** 从Oracle官网下载[Basic Package和SDK Package](https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html)[^5],解压到目标目录(例如`/opt/oracle`): ```bash unzip instantclient-basic-linux.x64-21.6.0.0.0dbru.zip unzip instantclient-sdk-linux.x64-21.6.0.0.0dbru.zip mv instantclient_21_6 /opt/oracle/ ``` - **设置环境变量** 在`~/.bashrc`中添加: ```bash export LD_LIBRARY_PATH=/opt/oracle/instantclient_21_6:$LD_LIBRARY_PATH export PATH=/opt/oracle/instantclient_21_6:$PATH ``` 执行`source ~/.bashrc`生效。 #### 2. 编写示例代码 创建一个C++文件`test_oci.cpp`,使用OCI(Oracle Call Interface)连接数据库: ```cpp #include <iostream> #include <oci.h> int main() { OCIEnv *env; OCIEnvCreate(&env, OCI_THREADED | OCI_OBJECT, nullptr, nullptr, nullptr, nullptr, 0, nullptr); std::cout << "OCI环境初始化成功!" << std::endl; return 0; } ``` #### 3. 编译命令 使用g++编译时需指定头文件路径和OCI库: ```bash g++ test_oci.cpp -o test_oci \ -I/opt/oracle/instantclient_21_6/sdk/include \ -L/opt/oracle/instantclient_21_6 \ -lclntsh ``` #### 4. 解决常见问题 - **错误:不到`oci.h`** 检查`-I`参数路径是否正确,确保SDK包已正确解压[^3]。 - **错误:`libclntsh.so`未到** 确认`LD_LIBRARY_PATH`已包含Instant Client路径,或运行: ```bash sudo sh -c "echo /opt/oracle/instantclient_21_6 > /etc/ld.so.conf.d/oracle.conf" sudo ldconfig ``` #### 5. 替代方案:使用OCILIB 若不想依赖Oracle官方库,可使用开源库[OCILIB](https://vrogier.github.io/ocilib/)[^1]: ```bash # 安装OCILIB sudo apt-get install libocilib-dev # 编译 g++ test_ocilib.cpp -o test_ocilib -locilib ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值