从 .so 链接 C++ 函数时出现问题 [重复]

Problem when linking C++ functions from .so [duplicate]

提问人:alvarella 提问时间:7/28/2022 最后编辑:273Kalvarella 更新时间:7/28/2022 访问量:34

问:

我的 HOME 中有以下文件夹结构:

- myproject/
    + src/
        + utilities.cpp
    + inc/
        + Executor.hpp
        + utilities.hpp
    + lib/
        + utilities.o
        + libutilities.so

- other_project/

    + main.cpp

utilities.hpp 包含:

#pragma once
#ifndef UTILITIES_HXX
#define UTILITIES_HXX

#include <string>
#include <iostream>
#include <map>
#include <vector>

namespace myproject{

void readFromCSV(const std::string& filename,char delimiter,const int nlines,std::map<int,std::vector<std::string>> & csv_contents);


}
#endif

和实用程序.cpp:

#include "utilities.hpp"
#include<fstream>
#include<algorithm>
#include<sstream>

const std::string readFileIntoString(const std::string & path){
        auto ss=std::ostringstream{};
        std::ifstream input_file(path);
        if(!input_file.is_open()){
                std::cout<<"Unable to open" << path;
                exit(0);
        }
        ss<<input_file.rdbuf();
        return ss.str();
}

void readFromCSV(const std::string& filename,char delimiter,const int nlines,std::map<int,std::vector<std::string>> & csv_contents){

        std::string file_contents=readFileIntoString(filename);
        std::istringstream sstream(file_contents);
        std::vector<std::string> items;
        std::string record;

        int counter=0;
        while(std::getline(sstream,record)&&(counter<nlines)){
                std::istringstream line(record);
                while(std::getline(line,record,delimiter)){
                        record.erase(std::remove_if(record.begin(), record.end(), isspace), record.end());
                        items.push_back(record);
                }
                csv_contents[counter]=items;
                items.clear();

                counter++;
        }

}

为了创建 libutilities.so 我在 $HOME/myproject 中执行了以下 bash 脚本:

g++ -I./inc -fPIC -c -o utilities.o src/utilities.cpp
gcc -shared -o libutilities.so utilities.o
mv libutilities.so lib/
mv utilities.o lib/

这编译得很好。所以我想在 other_project/main 中使用实用程序库.cpp:

#include "Executor.hpp"
#include "utilities.hpp"
using namespace myproject;
main(){
    //variable declaration and definition
    readFromCSV(setFilename(PATH_INPUT,SUFFIX_INPUT,batch_index),';',batches,p);
    //other code
}

因此,当我将其编译为:

g++ -o main main.o -I$(HOME)/myproject/inc -L$(HOME)/myproject/lib -lutilities 

在 Makefile 中,我收到以下错误:

/usr/bin/ld: main.o: in function `main()': /my_home_directory/other_project/main.cpp:168: undefined reference to myproject::readFromCSV(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, int, std::map<int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::less<int>, std::allocator<std::pair<int const, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >&)'
pgacclnk: child process exit status 1: /usr/bin/ld

有什么想法吗?

谢谢

C G++ 链接器错误

评论

1赞 pptaszni 7/28/2022
您在输出中看到了什么?也许问题在于使用会导致不同的名称混乱。nm -D libutilities.so | grep readFromCSVg++gcc
0赞 Aconcagua 7/28/2022
@pptaszni 宁愿不假设名称篡改 - 这应该在源文件的编译中已经完成 - 也使用了 g++。
0赞 Aconcagua 7/29/2022
您可能想阅读有关使用 namespace ... 的信息(大约有 ,但这实际上适用于所有命名空间)。std

答:

1赞 273K 7/28/2022 #1

在全局命名空间中声明函数,并在全局命名空间中定义函数。因此,您得到错误。readFromCSVnamespace myprojectreadFromCSVundefined reference to myproject::readFromCSV