ошибка компоновщика при смешивании объектных файлов, скомпилированных из C и C ++ - PullRequest
0 голосов
/ 22 мая 2011

У меня проблемы со смешиванием кода C и C ++, я отдельно компилирую код C и C ++, используя gcc и g ++.Код C вызывает функции C ++.Я собираю их отдельно, которые проходят хорошо.Я связываю оба объекта для создания файла ".so".когда я загружаю этот модуль из lua, загрузка модуля завершается неудачно, говоря какой-то неопределенный символ.Я вставляю C и C ++ и ошибку lua здесь.

File: wrap.c
----------------------------------------------
#include<stdio.h>

//extern "C"{
extern const char* getMobileFromUid(long long );
extern void addToUidHash(long long, char*);
//}
int callcpp (int ac, char **av)
{
    addToUidHash(1, "98866380587");
    fprintf(stderr,"hash:%s",getMobileFromUid(1));
    return 0;
}

File : uid_hash.cc
--------------------------------------------------------
#include <iostream>
#include <string>
#include <map>

typedef std::map<long long, std::string> uidMapType;
uidMapType uidMap;

extern "C"{ 
const char *getMobileFromUid(long long );
void         addToUidHash(long long, char *);
}


//Add the element  to the uid hash 
void 
addToUidHash(long long uid, char *mobile)
{
    uidMap[uid] = mobile;
    return;
}

//print the uid hash 
void 
printUidHash()
{
    uidMapType::const_iterator end = uidMap.end(); 
    for (uidMapType::const_iterator it = uidMap.begin(); it != end; ++it)
    {
        std::cout << "key = " << it->first;
        std::cout << " value = " << it->second << '\n';
    }
    return;
}


//get the mobile number string from the uid of the user 
const char *
getMobileFromUid(long long uid)
{
        uidMapType::iterator iter = uidMap.find(uid);
        if (iter == uidMap.end()) return NULL; 
        return iter->second.c_str();
}


I compile both of them like below 

ravit@ravit-laptop:~$ g++ -c uid_hash.cc -o uid_hash.o 
ravit@ravit-laptop:~$ gcc -c wrap.c -o wrap.o 
ravit@ravit-laptop:~$ ld -shared wrap.o uid_hash.o -o uid_hash.so 


after this i try to load the module from lua and i get an error "undefined" symbol 

ravit@ravit-laptop:~$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require "uid_hash"
error loading module 'uid_hash' from file './uid_hash.so':
    ./uid_hash.so: undefined symbol: _ZNSsaSEPKc
stack traceback:
    [C]: ?
    [C]: in function 'require'
    stdin:1: in main chunk
    [C]: ?
> 

что это за символ, который остается неопределенным?

1 Ответ

3 голосов
/ 22 мая 2011

Я думаю, вам нужно связаться с библиотекой C ++.C ++ Filt может помочь вам расшифровать искаженное имя символа:

main% c++filt _ZNSsaSEPKc
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...