在苹果笔记本操作系统里OS X10.8版本使用GCC来制作一个可执行文件的方法:

#include "WordOper.h"

#include <stdio.h>  //printf

#include <string.h>   //string,strlen,strcpy !

#include <iostream>
using namespace std;//string type

#include <fstream>

#include <sstream> //stringstream

//add this 2 lines, strlen still not ok!
#include <vector> // for get months

#include <time.h>

#include <stdlib.h> // string to int

//#include <windows.h>



WordOper::WordOper()
{
}

WordOper::WordOper(const WordOper& orig)
{
}

WordOper::~WordOper()
{
	//  delete[] buffer_char_p_temp;
}

const int WordOper::CreateFolder(const char* path)
{
    //	COleDateTime time = COleDateTime::GetCurrentTime();
// 	string path_name =path;
// 	if (!CreateDirectory(path_name.c_str(), NULL))
//         return 1; // Failed,it has been existed
// 	else
		return 0;// Successful, Create New Direct!
}

const char* WordOper::GetFileName(const char* full_path)
{
    int fileNamePos = 0;
    string full_path_str;
    full_path_str = full_path;
    fileNamePos = full_path_str.find_last_of("\\");
    fileNamePos ++;
    string str_FileName="";
    str_FileName.append(&full_path_str.at(fileNamePos));
    return str_FileName.c_str();
}

const char* WordOper::GetFileNamePrefix(const char* file_name)
{
    int fileNamePos = 0;
    /*
    string full_path_str="";
    full_path_str = file_name;
    fileNamePos = full_path_str.find_last_of(".");
    if(fileNamePos<=0)
    {
        return full_path_str.c_str();
    }
    else
    {
        char *value1=new char[fileNamePos];
        strncpy(value1,full_path_str.c_str(),fileNamePos);
        return value1;
    }
   */
   return NULL;
}

const int WordOper::CopyAFile(const char *dest_file_name,const char* source_file_name)
{
    fstream fsCopee(source_file_name,ios::binary|ios::in);
    fstream fsCoper(dest_file_name,ios::binary|ios::out);
    fsCoper<<fsCopee.rdbuf();
	return 0;
}
const char *WordOper::ReplaceString(const char *source,const char *find,
									 const char *replace)
{
    int find_length = strlen(find);//find.size();
    int replace_length  = strlen(replace);// replace.size();
    string::size_type find_location =0;
    string replace_result = source;
	find_location = replace_result.find(find, 0);
	int i=0;
	while(find_location != string::npos)
	{
		find_location = replace_result.find(find, i);
		if( find_location != string::npos)
		{
			replace_result.replace(find_location,find_length,replace);
			i = find_location + replace_length;
        }
	}

    static char *str = NULL; /* this only happens once */
	delete [] str;              /* delete previous cached version */
	str = new char[strlen(replace_result.c_str()) + 1]; /* allocate space for the string and it's NUL terminator */
	strcpy(str, replace_result.c_str());
	return str;
}

August 25, 2013 1:14:39 AM PDT

  1. 在苹果笔记本操作系统里OS X10.8版本使用GCC来制作一个可执行文件的方法:
  2. 原理:STEP1 编写一个C++类文件,编译成一个目标文件,然后编译成静态库文件。
  3.     STEP2 编写一个调用这个C++的主程序,然后编译成目标文件,然后联合静态库一起编译成一个可执行文件。
  4. STEP3 编译C++类成一个目标文件:打开终端控制台,然后进入到要编译的源代码目录里,输入
  5. “g++ -c -o WordOper.o WordOper.cpp” 释:将这个cpp文件编译成o标文件,为生成静态或者动态库服务
  6. STEP4 生成静态库a文件,在控制台里输入
  7. "libtool -static -o WordOper.a WordOper.o" //用上一步生成的o文件来生成a文件
  8. STEP5 编译C++主程序,在控制台里面输入
  9. “g++ -c -o test.o main.cpp” //编译主要调用的程序为目标文件
  10. STEP5 将静态库和主程序一起编译,生成一个可执行文件(在mac os x),在控制台里输入
  11. “g++ test.o -o main ./WordOper.a” //执行文件是main
  12. STEP6 to Run,in Terminal
  13. "./main"
  14. 就会看到输出,测试成功!
  15. 注释使用通用的C++类在主程序里面以类的方式调用对应的h文件,然后调用其函数就可以了。
  16. #include "WordOper.h" WordOper m_WordOper;
  17. 然后调用这个变量里的函数就可以了
  18. /* 
     * File:   main.cpp
     * Author: owner
     *
     * Created on 2013年8月21日, 下午12:13
     */
    //#include "wordoper/WordOper.h"
    #include "WordOper.h"
    
    #include <cstdlib>
    #include <iostream> // <iostream.h> is error in vc++6!
    using namespace std;
    #include <sstream> //stringstream
    
    int main(int argc, char* argv[])
    {
    	//printf("Hello World!\n");
    	
    	string text = "";
    
    	WordOper m_WordOper;
    
    	string file_name ="";
    	
    	file_name.append("text900000.txt");
    
    	if (m_WordOper.file_if_exist(file_name.c_str()))
    	{
    
    	}
    	else
    	{
           m_WordOper.CreateFile(file_name.c_str());
    	}	
    
    	for (int i=0;i<900;i++)
    	{
    		cout<<"No:"<<i<<endl;
    		text.append("No");
    		text.append(m_WordOper.Get_string(i));
    		text.append(" : ");
    		text.append(m_WordOper.Get_string(i));
    		text.append(" * ");
    		text.append(m_WordOper.Get_string(i+1));
    		text.append(" = ");
    		text.append(m_WordOper.Get_string(i*(i+1)));
    		text.append("\r\n");
    
            m_WordOper.AppendTxtToAFile(file_name.c_str(),text.c_str()); 
    		text = "";
    	}
    	
    	return 0;
    }
    
    
    
    

    C++类文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值