我如何使用我构建的库而不会从源代码中出错,但不为我自己的项目编译?
How can I use the library that I have built without error from source, but not compiling for my own project?
我想试试 AsmJit library。使用 'cmake' 和 'make' 从源代码构建它没有问题,它提供的示例都被完美地编译和执行。我还 make install
导出了依赖文件。
然后我想使用这个库编译我自己的程序,所以我检索了生成的文件(headers 和静态库)将它们添加到代码为 [=41 的新项目中=] 上给出的第一个例子 library's website:
// #define ASMJIT_NO_DEPRECATED // this line is no part of the original code
#include <asmjit/asmjit.h>
#include <stdio.h>
using namespace asmjit;
// Signature of the generated function.
typedef int (*Func)(void);
int main(int argc, char* argv[]) {
JitRuntime rt; // Runtime designed for JIT code execution.
CodeHolder code; // Holds code and relocation information.
code.init(rt.environment()); // Initialize CodeHolder to match JIT environment.
x86::Assembler a(&code); // Create and attach x86::Assembler to `code`.
a.mov(x86::eax, 1); // Move one to 'eax' register.
a.ret(); // Return from function.
// ----> x86::Assembler is no longer needed from here and can be destroyed <----
Func fn;
Error err = rt.add(&fn, &code); // Add the generated code to the runtime.
if (err) return 1; // Handle a possible error returned by AsmJit.
// ----> CodeHolder is no longer needed from here and can be destroyed <----
int result = fn(); // Execute the generated code.
printf("%d\n", result); // Print the resulting "1".
// All classes use RAII, all resources will be released before `main()` returns,
// the generated function can be, however, released explicitly if you intend to
// reuse or keep the runtime alive, which you should in a production-ready code.
rt.release(fn);
return 0;
}
这是我的测试项目的层次结构:
include\
asmjit\ // The generated headers from the library build
libasmjit.a // The generated static library from the library build
main.cpp // My program's code, as pasted above
这里是用于编译的命令行:
g++ main.cpp -o main -Iinclude -L -lasmjit
出现第一个编译错误:
In file included from include/asmjit/./core.h:2008,
from include/asmjit/asmjit.h:27,
from main.cpp:1:
include/asmjit/././core/builder.h:375:20: error: function 'asmjit::Error asmjit::BaseBuilder::dump(asmjit::String&, uint32_t) const' definition is marked dllimport
375 | ASMJIT_API Error dump(String& sb, uint32_t formatFlags = 0) const noexcept {
| ^~~~
在对 lib 代码进行一些研究后,定义 ASMJIT_NO_DEPRECATED
宏(参见上面代码中的第一条注释行)可以防止重现此错误。我怀疑这是个好主意,因为它可能是以下 link 错误的原因:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x35): undefined reference to `__imp__ZN6asmjit10JitRuntimeC1EPKNS_12JitAllocator12CreateParamsE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x45): undefined reference to `__imp__ZN6asmjit10CodeHolderC1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x6e): undefined reference to `__imp__ZN6asmjit10CodeHolder4initERKNS_11EnvironmentEy'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x82): undefined reference to `__imp__ZN6asmjit3x869AssemblerC1EPNS_10CodeHolderE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x11f): undefined reference to `__imp__ZN6asmjit3x869AssemblerD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x12f): undefined reference to `__imp__ZN6asmjit10CodeHolderD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x142): undefined reference to `__imp__ZN6asmjit10JitRuntimeD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x159): undefined reference to `__imp__ZN6asmjit3x869AssemblerD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x169): undefined reference to `__imp__ZN6asmjit10CodeHolderD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x17c): undefined reference to `__imp__ZN6asmjit10JitRuntimeD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text$_ZN6asmjit11BaseEmitter4emitIJRKNS_3x862GpEiEEEjjDpOT_[_ZN6asmjit11BaseEmitter4emitIJRKNS_3x862GpEiEEEjjDpOT_]+0x4c): undefined reference to `__imp__ZN6asmjit11BaseEmitter6_emitIEjRKNS_8Operand_ES3_'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text$_ZN6asmjit11BaseEmitter4emitIJEEEjjDpOT_[_ZN6asmjit11BaseEmitter4emitIJEEEjjDpOT_]+0x1b): undefined reference to `__imp__ZN6asmjit11BaseEmitter6_emitIEj'
collect2.exe: error: ld returned 1 exit status
除了定义这个宏,我找不到防止第一个错误发生的方法,而且我也不明白为什么 link 编辑器找不到引用。我还尝试将这些依赖项(headers + 静态库)放在适当的 MinGW 目录(即全局 include
和 lib
目录)中,但这不会改变任何东西。
如何编译这个程序,很简单?我也想知道为什么我做的没有用,知道了这个错误的原因,我以后可能会处理其他相同风格的人。
PS:我在管理 C 和 C++ 中的外部依赖方面经验不多。我在 Windows,使用 MinGW 和最新版本的 GCC。
您缺少 ASMJIT_STATIC
编译时定义 - 如果您静态使用 AsmJit,则必须定义它。 AsmJit 在编译时检查此定义以设置 ASMJIT_API
宏,该宏扩展为特定于编译器的 import/export/visibility 属性。
AsmJit 文档(构建说明部分)说 [1]:
Projects that use AsmJit statically must define ASMJIT_STATIC in all compilation units that use AsmJit, otherwise AsmJit would use dynamic library imports in ASMJIT_API decorator. The recommendation is to define this macro across the whole project that uses AsmJit this way.
因此在您的特定情况下,这应该可以解决问题:
g++ main.cpp -o main -Iinclude -L. -lasmjit -DASMJIT_STATIC
注意:文档 link 特意 link 到索引页,这样一旦文档被重新组织它就不会死 link。
我想试试 AsmJit library。使用 'cmake' 和 'make' 从源代码构建它没有问题,它提供的示例都被完美地编译和执行。我还 make install
导出了依赖文件。
然后我想使用这个库编译我自己的程序,所以我检索了生成的文件(headers 和静态库)将它们添加到代码为 [=41 的新项目中=] 上给出的第一个例子 library's website:
// #define ASMJIT_NO_DEPRECATED // this line is no part of the original code
#include <asmjit/asmjit.h>
#include <stdio.h>
using namespace asmjit;
// Signature of the generated function.
typedef int (*Func)(void);
int main(int argc, char* argv[]) {
JitRuntime rt; // Runtime designed for JIT code execution.
CodeHolder code; // Holds code and relocation information.
code.init(rt.environment()); // Initialize CodeHolder to match JIT environment.
x86::Assembler a(&code); // Create and attach x86::Assembler to `code`.
a.mov(x86::eax, 1); // Move one to 'eax' register.
a.ret(); // Return from function.
// ----> x86::Assembler is no longer needed from here and can be destroyed <----
Func fn;
Error err = rt.add(&fn, &code); // Add the generated code to the runtime.
if (err) return 1; // Handle a possible error returned by AsmJit.
// ----> CodeHolder is no longer needed from here and can be destroyed <----
int result = fn(); // Execute the generated code.
printf("%d\n", result); // Print the resulting "1".
// All classes use RAII, all resources will be released before `main()` returns,
// the generated function can be, however, released explicitly if you intend to
// reuse or keep the runtime alive, which you should in a production-ready code.
rt.release(fn);
return 0;
}
这是我的测试项目的层次结构:
include\
asmjit\ // The generated headers from the library build
libasmjit.a // The generated static library from the library build
main.cpp // My program's code, as pasted above
这里是用于编译的命令行:
g++ main.cpp -o main -Iinclude -L -lasmjit
出现第一个编译错误:
In file included from include/asmjit/./core.h:2008,
from include/asmjit/asmjit.h:27,
from main.cpp:1:
include/asmjit/././core/builder.h:375:20: error: function 'asmjit::Error asmjit::BaseBuilder::dump(asmjit::String&, uint32_t) const' definition is marked dllimport
375 | ASMJIT_API Error dump(String& sb, uint32_t formatFlags = 0) const noexcept {
| ^~~~
在对 lib 代码进行一些研究后,定义 ASMJIT_NO_DEPRECATED
宏(参见上面代码中的第一条注释行)可以防止重现此错误。我怀疑这是个好主意,因为它可能是以下 link 错误的原因:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x35): undefined reference to `__imp__ZN6asmjit10JitRuntimeC1EPKNS_12JitAllocator12CreateParamsE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x45): undefined reference to `__imp__ZN6asmjit10CodeHolderC1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x6e): undefined reference to `__imp__ZN6asmjit10CodeHolder4initERKNS_11EnvironmentEy'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x82): undefined reference to `__imp__ZN6asmjit3x869AssemblerC1EPNS_10CodeHolderE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x11f): undefined reference to `__imp__ZN6asmjit3x869AssemblerD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x12f): undefined reference to `__imp__ZN6asmjit10CodeHolderD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x142): undefined reference to `__imp__ZN6asmjit10JitRuntimeD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x159): undefined reference to `__imp__ZN6asmjit3x869AssemblerD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x169): undefined reference to `__imp__ZN6asmjit10CodeHolderD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x17c): undefined reference to `__imp__ZN6asmjit10JitRuntimeD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text$_ZN6asmjit11BaseEmitter4emitIJRKNS_3x862GpEiEEEjjDpOT_[_ZN6asmjit11BaseEmitter4emitIJRKNS_3x862GpEiEEEjjDpOT_]+0x4c): undefined reference to `__imp__ZN6asmjit11BaseEmitter6_emitIEjRKNS_8Operand_ES3_'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text$_ZN6asmjit11BaseEmitter4emitIJEEEjjDpOT_[_ZN6asmjit11BaseEmitter4emitIJEEEjjDpOT_]+0x1b): undefined reference to `__imp__ZN6asmjit11BaseEmitter6_emitIEj'
collect2.exe: error: ld returned 1 exit status
除了定义这个宏,我找不到防止第一个错误发生的方法,而且我也不明白为什么 link 编辑器找不到引用。我还尝试将这些依赖项(headers + 静态库)放在适当的 MinGW 目录(即全局 include
和 lib
目录)中,但这不会改变任何东西。
如何编译这个程序,很简单?我也想知道为什么我做的没有用,知道了这个错误的原因,我以后可能会处理其他相同风格的人。
PS:我在管理 C 和 C++ 中的外部依赖方面经验不多。我在 Windows,使用 MinGW 和最新版本的 GCC。
您缺少 ASMJIT_STATIC
编译时定义 - 如果您静态使用 AsmJit,则必须定义它。 AsmJit 在编译时检查此定义以设置 ASMJIT_API
宏,该宏扩展为特定于编译器的 import/export/visibility 属性。
AsmJit 文档(构建说明部分)说 [1]:
Projects that use AsmJit statically must define ASMJIT_STATIC in all compilation units that use AsmJit, otherwise AsmJit would use dynamic library imports in ASMJIT_API decorator. The recommendation is to define this macro across the whole project that uses AsmJit this way.
因此在您的特定情况下,这应该可以解决问题:
g++ main.cpp -o main -Iinclude -L. -lasmjit -DASMJIT_STATIC
注意:文档 link 特意 link 到索引页,这样一旦文档被重新组织它就不会死 link。