Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
混合从C和C编译的对象文件时发生链接器错误++; P>混合C和C++代码有困难,我分别使用GCC和G++编译C和C++代码。C代码调用C++函数。我分别编译了它们,它们都能顺利通过。我链接这两个对象以生成一个“.so”文件。当我从lua加载这个模块时,模块加载失败,因为出现了一些未定义的符号。我粘贴C和C++和LUA错误在这里。_C++_Lua - Fatal编程技术网

混合从C和C编译的对象文件时发生链接器错误++; P>混合C和C++代码有困难,我分别使用GCC和G++编译C和C++代码。C代码调用C++函数。我分别编译了它们,它们都能顺利通过。我链接这两个对象以生成一个“.so”文件。当我从lua加载这个模块时,模块加载失败,因为出现了一些未定义的符号。我粘贴C和C++和LUA错误在这里。

混合从C和C编译的对象文件时发生链接器错误++; P>混合C和C++代码有困难,我分别使用GCC和G++编译C和C++代码。C代码调用C++函数。我分别编译了它们,它们都能顺利通过。我链接这两个对象以生成一个“.so”文件。当我从lua加载这个模块时,模块加载失败,因为出现了一些未定义的符号。我粘贴C和C++和LUA错误在这里。,c++,lua,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,

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]: ?
> 
文件:wrap.c
----------------------------------------------
#包括
//外部“C”{
外部常量字符*getMobileFromUid(长);
extern void addToUidHash(long-long,char*);
//}
int callcpp(int ac,字符**av)
{
addToUidHash(1,“98866380587”);
fprintf(stderr,“哈希:%s”,getMobileFromUid(1));
返回0;
}
文件:uid\u hash.cc
--------------------------------------------------------
#包括
#包括
#包括
typedef std::map uidMapType;
uidMap类型uidMap;
外部“C”{
常量字符*getMobileFromUid(长);
void addtouiddhash(long-long,char*);
}
//将元素添加到uid哈希中
无效的
addToUidHash(长uid,字符*移动)
{
uidMap[uid]=移动设备;
返回;
}
//打印uid哈希
无效的
printUidHash()
{
uidMapType::const_迭代器end=uidMap.end();
对于(uidMapType::const_迭代器it=uidMap.begin();it!=end;++it)
{
标准::cout

这个符号是什么?它仍然没有定义?

< P>我想你需要链接到C++库。C++ FLT可以帮助你破解被损坏的符号名称:

main% c++filt _ZNSsaSEPKc
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)
main%c++过滤器
标准::基本字符串::运算符=(字符常量*)

> p>我想你需要链接到C++库。C++ FLT可以帮助你破解被损坏的符号名称:

main% c++filt _ZNSsaSEPKc
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)
main%c++过滤器
标准::基本字符串::运算符=(字符常量*)

您还需要链接libc,可能还有STL,对吧?它们不会神奇地包含在内,您需要将它们添加到链接器行中。您还可以将代码块分开吗?即C/C++代码,然后是描述,然后是命令,然后是描述,然后是命令/输出?很难读取和编辑空白,因为r我想除了你以外的任何人(至少在不改变某些文本的情况下)你能不能给C和C++库提供LD标志?你还需要链接LBC和STL,对吗?它们没有神奇地包含在内,你需要把它们添加到链接器中。你也可以把代码块分开吗?C++,C++代码,然后描述,然后命令,然后描述,然后命令/输出?这很难。我想,除了你之外,阅读和编辑空白是不可能的(至少在不改变某些文本的情况下)你能不能给我C++和C++库的LD标志?谢谢你给我一个很好的提示,我在链接阶段没有链接STD C++库。我如何在Linux上链接标准C++和标准C库?我用LD命令来连接,而不是GCC。你可以用G+代替LD链接。G++知道它所需要的所有库。ANK是一个很棒的技巧,我在链接阶段没有链接STD C++库。我如何在Linux上链接标准C++和标准C库?我用LD命令来链接,而不是GCC。你可以用G+来代替LD链接。G++知道它所需要的所有库。