正如标题所说,我想从三个 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.
最佳答案
ld -r file1.o file2.o file3.o -o file.o
你应该使用 g++
链接,而不是调用ld
直接地。路过.o
GCC 的文件将导致它为您调用链接器,所以这是可行的:
g++ file1.o file2.o file3.o -o file.o
你说:
but I don't know how to compile .cpp files into .o files.
你只需要编译 -c
(如果你想把目标文件放在共享库中,那么 -fPIC
):
g++ -c file1.cpp -fPIC -o file1.o
but I must also provide static library to this command
不,因为带有 -c
的命令是编译,不是链接,所以不能提供库,因为它们只在链接时使用。
So, I want shared library with all symbols from static library (libAlgatorc.a) and from all three cpp files (file1.cpp, file2.cpp and file3.cpp)
那么你需要阅读我已经指出你的答案:https://stackoverflow.com/a/2649792/981959
也许您应该阅读有关在类 unix 环境中构建软件的教程,可能类似于 An Introduction to GCC以便您了解所需的单独步骤以及每个命令的作用。
编译每个文件:
g++ -c -fPIC file1.cpp
g++ -c -fPIC file2.cpp
g++ -c -fPIC file3.cpp
(此处不需要-o
选项,默认情况下,使用file1.cpp
选项时,GCC 会将file1.o
等文件编译为-c
。)
或者,您可以一步完成:
g++ -c -fPIC file1.cpp file2.cpp file3.cpp
你不能使用-o
选项在这里,因为有三个不同的.o
作为 -c
的输出生成的文件步骤,因此您不能指定单个输出文件。
将它们全部链接到一个共享库中,该库还包括 libAlgatorc.a
中的符号:
g++ file1.o file2.o file3.o -shared -o libProject.so -Wl,--whole-archive libAlgatorc.a -Wl,--no-whole-archive
或者在一个命令中编译所有三个文件然后链接它们(注意这里没有 -c
选项):
g++ -fPIC file1.cpp file2.cpp file3.cpp -shared -o libProject.so -Wl,--whole-archive -lAlgatorc -Wl,--no-whole-archive
注意说 -I /usr/local/include
是多余的或 -L /usr/local/include
因为无论如何,这些路径总是默认搜索。
更简单的方法是编写一个makefile:
libProjects.so: file1.o file2.o file3.o
$(CXX) -shared $^ -o $@ -Wl,--whole-archive -lAlgatorc -Wl,--no-whole-archive
file1.o file2.o file3.o : CXXFLAGS+=-fPIC
这就是你在 makefile 中所需要的,因为 Make 已经知道如何创建 file1.o
来自输入 file1.cpp
(并为 CXXFLAGS
目标设置 .o
可确保在编译这些文件时使用 -fPIC
)。由于您对执行此操作的正确命令没有信心,我建议依靠 Make 来为您提供正确的命令。
关于c++ - 使用 g++ 从 cpp 文件和静态库创建共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541451/