LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1439|回复: 3

如何生成lib*.so文件???

[复制链接]
发表于 2009-3-2 16:27:20 | 显示全部楼层 |阅读模式
#define Uses_SCIM_LOOKUP_TABLE
#define Uses_SCIM_FRONTEND

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <scim.h>
#include <locale.h>
#include <sstream>
#include <iostream>
using namespace scim;
class MyFrontEnd: public FrontEndBase {
private:
        int id;
private:
        WideString factory_info(String uuid) {
                std::basic_stringstream<wchar_t> stream;
                stream << "Name:" << get_factory_name(uuid).c_str() << ' ';
                stream << "Language:" << get_factory_language(uuid).c_str() << ' ';
                stream << "Authors:" << get_factory_authors(uuid).c_str() << ' ';
                stream << "Credits:" << get_factory_credits(uuid).c_str() << ' ';
                //stream << "Help:"<<get_factory_help(uuid).c_str()<<' ';
                //stream << "Icon_file:"<<get_factory_icon_file(uuid).c_str()<<' ';
                stream << "Locales:" << get_factory_locales(uuid).c_str() << ' ';
                //stream<<'\n';
                return stream.str();
        }

        WideString instance_info(int id) {
                std::basic_stringstream<wchar_t> stream;
                stream << "Name:" << get_instance_name(id).c_str() << ' ';
                stream << "Encoding:" << get_instance_encoding(id).c_str() << ' ';
                stream << "Authors:" << get_instance_authors(id).c_str() << ' ';
                stream << "Credits:" << get_instance_credits(id).c_str() << ' ';
                //stream << "Help:"<<get_instance_help(id).c_str()<<' ';
                //stream << "Icon_file:"<<get_instance_icon_file(id).c_str()<<' ';
                stream << "UUID:" << get_instance_uuid(id).c_str() << ' ';
                //stream<<'\n';
                return stream.str();
        }

        void dump_factory() {
                std::vector<String> uuids;
                String language;
                uint32 ret = get_factory_list_for_language(uuids, language);
                if (ret > 0) {
                        std::vector<String>::const_iterator it;
                        for (it = uuids.begin(); it != uuids.end();++it) {
                                printf("%S\n",factory_info(*it).c_str() );
                        }
                }
        }
        void test_key(const KeyEvent& key) {
                focus_in(id);
                if(process_key_event (id, key)) {
                        printf("rocess key success\n");
                } else {
                        perror("rocess key error!\n");
                }
        }
public:

        MyFrontEnd(const BackEndPointer &backend) :
        FrontEndBase(backend) {
                setlocale(LC_ALL, "zh_CN.UTF-8");
        }

        void init(int argc, char **argv) {
                String uuid;
                String language="zh_CN";
                String encoding="UTF-8";
                uuid=get_default_factory (language, encoding);

                id= new_instance (uuid, encoding);
                printf("Default IM:%S\n",instance_info(id).c_str());

        }
        void run () {

                if(id==-1) {
                        perror("new_instance Error\n");
                }
                else {
                        printf("Get IM success\n");
                        test_key(KeyEvent("a"));
                        test_key(KeyEvent("n"));
                        test_key(KeyEvent("n"));
                        test_key(KeyEvent("n"));
                        test_key(KeyEvent("n"));
                }
        }

        void update_aux_string(int id, const WideString & str, const AttributeList & attrs)
        {
                printf("In update_aux_string -> Get string %S\n",str.c_str());

        }
        void update_preedit_string (int id, const WideString & str, const AttributeList & attrs)
        {
                printf("In update_preedit_string-> Get string %S\n",str.c_str());
        }
        void
        update_lookup_table   (int id, const LookupTable & table)
        {
                printf("In update_lookup_table\n");
                size_t n=table.number_of_candidates();
                for(size_t i=0;i<n;++i){
                        printf("%d->%S ",i,table.get_candidate(i).c_str());
                        if(i>0 && i%10 ==0 ){
                                printf("\n");
                        }
                }

        }

};

                        //Module Interface
static Pointer<MyFrontEnd> _scim_frontend(0);
static int _argc;
static char **_argv;

extern "C" {
void scim_module_init(void) {
        SCIM_DEBUG_FRONTEND(1) << "Initializing Socket FrontEnd module...\n";
}

void scim_module_exit(void) {
        SCIM_DEBUG_FRONTEND(1) << "Exiting Socket FrontEnd module...\n";
        _scim_frontend.reset();
}

void scim_frontend_module_init(const BackEndPointer &backend,
                const ConfigPointer &config, int argc, char **argv) {
        if (_scim_frontend.null()) {
                SCIM_DEBUG_FRONTEND(1) << "Initializing Socket FrontEnd module (more)...\n";
                _scim_frontend = new MyFrontEnd(backend);
                _argc = argc;
                _argv = argv;
        }
}

void scim_frontend_module_run(void) {
        if (!_scim_frontend.null()) {
                SCIM_DEBUG_FRONTEND(1) << "Starting Socket FrontEnd module...\n";
                _scim_frontend->init(_argc, _argv);
                _scim_frontend->run();
        }
}
}


谁知道这代码怎么生成

lib*.so文件??
发表于 2009-3-2 18:04:01 | 显示全部楼层

说清楚点.

使用gcc -fpic选项使得输出对像的模块是按照可重定位地址方式生成的,例如:
gcc -c -fpic helloworld.c
这样就会在当前目录中生成helloworld.o这个文件,然后再使用-shared 选项就可以生成共享库
gcc -shared helloworld.o -o helloworld.so

也可以直接一条命令完成
gcc -fpic -shared helloworld.c -o hello.so
c++应该也是g++吧.
我不知道你是不是这个意思?
回复 支持 反对

使用道具 举报

发表于 2009-3-4 18:01:57 | 显示全部楼层
上述代码存于test.cpp中

sudo apt-get install libscim-dev
g++ -fpic -shared  `pkg-config --libs --cflags scim`  test.cpp -o libtest.so

专业点,清除无用符号信息. 还能降低so文件的大小
strip libtest.so
回复 支持 反对

使用道具 举报

 楼主| 发表于 2009-3-6 16:50:30 | 显示全部楼层
谢谢,我弄好了~!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表