c-free配置c++11

1> 首先下载TDM-GCC-64。 网址为:http://sourceforge.net/projects/tdm-gcc/?source=typ_redirect


2>打开c-free--构建--构建选项--点击构建配置的右上角,如图。




3>点击新建配置--MinGW--名称自己起。  如图。



4>点击编译--把下方原始参数改为: -std=c++11 -DDEBUG    如图。




5>点击路径--点击删除全部路径--自动检测--找到TDM-GCC-64的路径--双击找到的路径--确定。如图。






至此,应该就能用了,留一个多线程的例子,进行测试。


ThreadEx.h


#ifndef ThreadEx__h
#define ThreadEx__h

#include <string>

using namespace std;

class ThreadEx
{
  public:
   //构造函数 
   ThreadEx();    
   //线程任务方法 
   void taskForThread(string preFix,string afterFix);   	
};

#endif

ThreadEx.cpp


#include "ThreadEx.h"
#include <thread> 
#include <iostream>  
#include <chrono>  

using namespace std;

ThreadEx::ThreadEx()
{
	 thread t1(&ThreadEx::taskForThread,this,"[","]");	
	 thread t2(&ThreadEx::taskForThread,this,"<",">");		 

     //要点:
	 //1、主线程不能先于子线程任务完成,否则子线程任务走不完就拉倒了。
	 //2、主线程中启动子线程后(采用局部对象法),要么join,要么detach。 
	 // 这是因为线程对象在被析构(~thread())之前必须调用一次join或detach,
	 // 否则运行报错的。
	 //官方英文解释如下:
	 //The trouble you are encountering is a result of the stopThread going out of scope on the stack. 
	 //The C++ standard has the following to say about this:
	 //30.3.1.3 thread destructor [thread.thread.destr] 
	 //~thread();
	 //If joinable() then terminate(), otherwise no effects. 
	 //[ Note: Either implicitly detaching or joining ajoinable() thread in its destructor could result in difficult to debug correctness (for detach) 
	 //or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed 
	 //while the thread is still joinable. — end note ] 
	 //What this means is that you should not let threads go out of scope without first calling either join() ordetatch().
     
	 //----------------------------------------------------------- 
	 //这里的休息模拟主线程干活 
	 //std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	 //主线程启动子线程后调用join方法,确保
	 //子线程和主线程合并,否则运行报错 
	 //但是这样主线程和子线程之间实际就不是并发了
	 //主线程会一直等待,直至子线程运行结束 
	 //另外当调用join函数时,调用线程阻塞等待目标线程终止,然后回收目标线程的资源。 
	 //t1.join();
	 //t2.join(); 
	 //-----------------------------------------------------------
	 
	 
	 
	 //-----------------------------------------------------------
	 //detach方法功能为将子线程分离,交由操作系统处理。
	 //当子线程主函数执行完之后,线程就结束了,运行时库负责清理与该线程相关的资源。  
	 t1.detach();
	 t2.detach(); 	 
	 //这里的休息模拟主线程干活 
	 std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	 //-----------------------------------------------------------
	 
	 
	 cout<<endl<<"main end"<<endl;
}

void ThreadEx::taskForThread(string preFix,string afterFix)
{
	for(int i=0;i<100;i++)
	{
		cout<<preFix<<i<<afterFix;
		//休眠20毫秒
		std::this_thread::sleep_for(std::chrono::milliseconds(20)); 
	}
}


main.cpp


#include <iostream>
#include <thread> 
#include <chrono>  
#include "ThreadEx.h"

using namespace std;


int main(int argc, char *argv[])
{
	new ThreadEx();	
	return 0;
}


大家可以新建一个项目进行测试,如果碰到问题,请自行搜索,因为,我也不懂,我也不懂,我也不懂。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在C++中使用OpenSSL库进行RSA加密和解密,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了OpenSSL库并且正确配置了开发环境。 2. 在您的C++代码中,包含OpenSSL的头文件: ```cpp #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/rsa.h> #include <openssl/pem.h> ``` 3. 生RSA密钥对: ```cpp RSA* generateRSAKey(int keyLength) { RSA* rsa = NULL; BIGNUM* bn = NULL; bn = BN_new(); if (bn == NULL) { return NULL; } if (BN_set_word(bn, RSA_F4) != 1) { BN_free(bn); return NULL; } rsa = RSA_new(); if (rsa == NULL) { BN_free(bn); return NULL; } if (RSA_generate_key_ex(rsa, keyLength, bn, NULL) != 1) { RSA_free(rsa); BN_free(bn); return NULL; } BN_free(bn); return rsa; } ``` 4. 使用公钥加密: ```cpp std::string rsaEncrypt(RSA* rsa, const std::string& plaintext) { int rsaSize = RSA_size(rsa); int encryptedSize = 0; std::string encryptedText(rsaSize, '\0'); encryptedSize = RSA_public_encrypt(plaintext.length(), reinterpret_cast<const unsigned char*>(plaintext.c_str()), reinterpret_cast<unsigned char*>(&encryptedText[0]), rsa, RSA_PKCS1_PADDING); if (encryptedSize == -1) { return ""; } encryptedText.resize(encryptedSize); return encryptedText; } ``` 5. 使用私钥解密: ```cpp std::string rsaDecrypt(RSA* rsa, const std::string& ciphertext) { int rsaSize = RSA_size(rsa); int decryptedSize = 0; std::string decryptedText(rsaSize, '\0'); decryptedSize = RSA_private_decrypt(ciphertext.length(), reinterpret_cast<const unsigned char*>(ciphertext.c_str()), reinterpret_cast<unsigned char*>(&decryptedText[0]), rsa, RSA_PKCS1_PADDING); if (decryptedSize == -1) { return ""; } decryptedText.resize(decryptedSize); return decryptedText; } ``` 请注意,这只是一个简单的示例,供您参考。在实际使用中,您可能需要进行更多的错误处理和参数验证。 希望这可以帮助到您!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值