就像标题所说,我想从三个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.
.o
文件添加到答案中的命令即可。关键是另一个答案展示了如何确保静态库中的所有符号最终都出现在共享库中,这正是你的问题所在。 - Jonathan Wakelysome.o another.o
,这完全相同。 - Jonathan Wakelyg++ ./libsomething.cpp -fPIC -shared -o ./libsomething.so
。 - Martin Thoma