使用 g++ 从 cpp 文件和静态库创建共享库
Create shared library from cpp files and static library with g++
正如标题所说,我想从三个 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 中定义但在 [= 中实现的 class 41=].a 因为 -c 我不能说 -L /usr/local/lib -lAlgatorc
最后,我想要这三个 classes 的共享库,以便在 main 函数中我可以将这个库加载到我的程序中,并且我可以从这三个 classes 调用方法.所以,我想要共享库,其中包含静态库 (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++
到 link,而不是直接调用 ld
。将 .o
文件传递给 GCC 将导致它为您调用 linker,所以这有效:
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
的命令正在编译,而不是linking,所以你不能提供库,因为它们只在linking时使用。
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)
那么你需要阅读我已经为你指出的答案:
也许您应该阅读有关在类 unix 环境中构建软件的教程,也许是 An Introduction to GCC 之类的内容,以便您了解所需的单独步骤以及每个命令的作用。
编译每个文件:
g++ -c -fPIC file1.cpp
g++ -c -fPIC file2.cpp
g++ -c -fPIC file3.cpp
(这里不需要-o
选项,当你使用-c
选项时,GCC默认会把file1.cpp
这样的文件编译成file1.o
.)
或者,您可以一步完成:
g++ -c -fPIC file1.cpp file2.cpp file3.cpp
你不能在这里使用-o
选项,因为-c
输出了三个不同的.o
文件步骤,因此您不能指定单个输出文件。
将它们 link 全部放入一个共享库中,该库还包括来自 libAlgatorc.a
:
的符号
g++ file1.o file2.o file3.o -shared -o libProject.so -Wl,--whole-archive libAlgatorc.a -Wl,--no-whole-archive
或者在将编译所有三个文件然后 link 它们的单个命令中(注意这里没有 -c
选项):
g++ -fPIC file1.cpp file2.cpp file3.cpp -shared -o libProject.so -Wl,--whole-archive -lAlgatorc -Wl,--no-whole-archive
N.B。说 -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.cpp
创建 file1.o
(并为 .o
目标设置 CXXFLAGS
确保编译这些文件时将使用 -fPIC
)。由于您对执行此操作的正确命令没有信心,我建议您依靠 Make 来获得适合您的命令。
正如标题所说,我想从三个 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 中定义但在 [= 中实现的 class 41=].a 因为 -c 我不能说 -L /usr/local/lib -lAlgatorc
最后,我想要这三个 classes 的共享库,以便在 main 函数中我可以将这个库加载到我的程序中,并且我可以从这三个 classes 调用方法.所以,我想要共享库,其中包含静态库 (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++
到 link,而不是直接调用 ld
。将 .o
文件传递给 GCC 将导致它为您调用 linker,所以这有效:
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
的命令正在编译,而不是linking,所以你不能提供库,因为它们只在linking时使用。
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)
那么你需要阅读我已经为你指出的答案:
也许您应该阅读有关在类 unix 环境中构建软件的教程,也许是 An Introduction to GCC 之类的内容,以便您了解所需的单独步骤以及每个命令的作用。
编译每个文件:
g++ -c -fPIC file1.cpp
g++ -c -fPIC file2.cpp
g++ -c -fPIC file3.cpp
(这里不需要-o
选项,当你使用-c
选项时,GCC默认会把file1.cpp
这样的文件编译成file1.o
.)
或者,您可以一步完成:
g++ -c -fPIC file1.cpp file2.cpp file3.cpp
你不能在这里使用-o
选项,因为-c
输出了三个不同的.o
文件步骤,因此您不能指定单个输出文件。
将它们 link 全部放入一个共享库中,该库还包括来自 libAlgatorc.a
:
g++ file1.o file2.o file3.o -shared -o libProject.so -Wl,--whole-archive libAlgatorc.a -Wl,--no-whole-archive
或者在将编译所有三个文件然后 link 它们的单个命令中(注意这里没有 -c
选项):
g++ -fPIC file1.cpp file2.cpp file3.cpp -shared -o libProject.so -Wl,--whole-archive -lAlgatorc -Wl,--no-whole-archive
N.B。说 -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.cpp
创建 file1.o
(并为 .o
目标设置 CXXFLAGS
确保编译这些文件时将使用 -fPIC
)。由于您对执行此操作的正确命令没有信心,我建议您依靠 Make 来获得适合您的命令。