【发布时间】:2015-10-11 01:10:08
【问题描述】:
正如标题所说,我想从三个 cpp 文件和一些静态库创建共享库。
基本上我想这样做
g++ libProject.so file1.cpp file2.cpp file3.cpp -I /usr/local/include -L /usr/local/lib -lAlgatorc
这是我的 file1.cpp:
#include <iostream>
#include <TestSetIterator.hpp>
class SortingTestSetIterator : public TestSetIterator {
public:
TestCase *get_current(){
//TODO: implement method
return nullptr;
}
};
这是我的 file2.cpp
#include<iostream>
#include<TestCase.hpp>
#include<Entity.hpp>
#include<ParameterSet.hpp>
class SortingTestCase : public TestCase {
public:
std::string print() {
//TODO: implement method
return "";
}
};
这是我的 file3.cpp
#include <iostream>
#include <AbsAlgorithm.hpp>
#include "SortingTestCase.cpp"
class SortingAbsAlgorithm : public AbsAlgorithm {
private:
SortingTestCase Sorting_test_case;
public:
bool init (TestCase &test) {
//TODO: implement method
return true;
}
void run() {
//TODO: Implement method
}
void done() {
//TODO: Implement method
}
};
我认为我需要创建三个.o文件(file1.o file2.o file3.o)然后像这样组合它们
ld -r file1.o file2.o file3.o -o file.o
当我有 file.o 时,我想我需要这样说:
g++ -shared -o libProject.so file.o
但我不知道如何将 .cpp 文件编译成 .o 文件。 我知道我可以这样做
g++ -std=gnu++11 -o file1.o -fPIC -c file1.cpp -I /usr/local/include
但我还必须为此命令提供静态库,因为 file1.cpp(和其他文件)继承了 /usr/local/include/TestSetIterator.hpp 中定义但在 /usr/local/lib/ 中实现的类libAlgatorc.a 因为 -c 我不能说 -L /usr/local/lib -lAlgatorc
最后,我想要这三个类的共享库,以便在主函数中我可以将这个库加载到我的程序中,并且我可以从这三个类中调用方法。所以,我想要包含来自静态库(libAlgatorc.a)和所有三个 cpp 文件(file1.cpp、file2.cpp 和 file3.cpp)的所有符号的共享库
我使用的是 Ubuntu 12.04 x64。
> g++ --version
g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
【问题讨论】:
-
这不是我已经向您指出的问题吗? stackoverflow.com/questions/2649735/…
-
没有。没有从静态库和 cpp 文件构建的共享库。仅从静态库构建共享库。我想把这两者结合起来。
-
还是基本一样。只需将
.o文件添加到该答案中的命令即可。关键是另一个答案显示了如何确保静态库中的所有符号最终都在共享库中,这正是您的问题。 -
其实一模一样,如果您阅读问题,您会看到链接器命令包含文件:
some.o another.o -
我来的时候正在找
g++ ./libsomething.cpp -fPIC -shared -o ./libsomething.so
标签: c++ linker g++ shared-libraries static-libraries