[英]C++ Wrapper for C library as shared library
I SOLVED THIS ISSUE BY MYSELF 我自己解决了这个问题
The problem was the linkage of the library. 问题是图书馆的联系。 I copied the libmywrapper.so(i renamed it) file to /usr/lib and linked with -mywrapper That's it :-)
我将libmywrapper.so(我将其重命名)文件复制到/ usr / lib并与-mywrapper链接就是这样:-)
Original post: 原始帖子:
I'm writing a wrapper library that allows to call C++ functions out of C-code. 我正在编写一个包装库,该包装库允许从C代码中调用C ++函数。 Unfortnuately it doesn't link...
不幸的是,它没有链接...
wrapper.h: wrapper.h:
#ifdef __cplusplus
extern "C"
{
#endif
extern char* (keygen) ();
#ifdef __cplusplus
}
#endif
wrapper.cpp: wrapper.cpp:
#include "wrapper.h"
#include <someincludes>
char* keygen ()
{
urandom u;
Makefile: Makefile文件:
TARGET := ./mywrapperlib.so
CXXFLAGS := -fPIC -shared -g -Wall -std=c++0x -I../someincludes -I.
CXX := g++
LIB := -lsomelibs
EXT := cpp
BUILDDIR := build
override BUILDDIR := $(strip $(BUILDDIR))
SOURCES := $(wildcard *.$(EXT))
OBJECTS := $(patsubst %.$(EXT), $(BUILDDIR)/%.o, $(SOURCES))
DEPS := $(patsubst %.$(EXT), $(BUILDDIR)/%.dep, $(SOURCES))
.PHONY: all
all: $(TARGET)
$(TARGET): $(OBJECTS) $(DEPS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJECTS) $(LIBS)
ifneq ($(MAKECMDGOALS), clean)
-include $(DEPS)
endif
$(OBJECTS): $(BUILDDIR)/%.o: %.$(EXT) $(BUILDDIR)/%.dep $(BUILDDIR)/.tag
$(CXX) $(CXXFLAGS) -c $< -o $@
$(DEPS): $(BUILDDIR)/%.dep: %.$(EXT) $(BUILDDIR)/.tag
mkdir -p $(dir $(@))
$(CXX) $(CXXFLAGS) -MM $< -MT $@ -MT $(<:.$(EXT)=.o) -o $@
%.tag:
mkdir -p $(dir $(@))
touch $@
.PHONY: clean
clean:
$(RM) -r $(BUILDDIR)
A test file that should use the library: test.c: 应使用以下库的测试文件:test.c:
#include <wrapper.h>
int main()
{
char* test = keygen();
}
When i try to compile it with 当我尝试用
gcc -o test.a -g -Iinclude -Llib/mywrapperlib.so test.c
I get 我懂了
/tmp/ccB9bEot.o: In function `main':
/some/paths/test.c:7: undefined reference to `keygen'
Im very unexperienced with mixing C & C++ code and writing libraries. 我对混合C&C ++代码和编写库没有经验。 Now im stuck and hope that someone can help me with this problem.
现在即时通讯卡住了,希望有人可以帮助我解决这个问题。
EDIT: 编辑:
I checked the lib with nm: 我用nm检查了lib:
nm lib/cryptdbwrapperlib.so | grep keygen
0000000000006935 T keygen
So, i guess that the problem is the linkage... 所以,我想问题是联系...
It's to do with the order of your flags to gcc
. 这与您标记
gcc
的顺序有关。
Do this: 做这个:
gcc -o test.a -g -Iinclude test.c -Llib/mywrapperlib.so
# ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
# first second
GCC reads libraries and objects left-to-right and (basically) ignores any that aren't needed "yet". GCC从左到右读取库和对象,并且(基本上)忽略“还”不需要的任何库和对象。 With my proposed change,
test.c
goes first so GCC knows that it's going to be looking for a symbol keygen
; 在我建议的更改中,
test.c
首先执行,所以GCC知道它将要寻找符号keygen
; then, when it finally sees -Llib/mywrapperlib.so
it scans it for keygen
, finds it, and knows that this library is required. 然后,当它最终看到
-Llib/mywrapperlib.so
时,将对其进行扫描以查找keygen
,找到它,并知道此库是必需的。
Change the function's signature in wrapper.cpp
to 将
wrapper.cpp
的函数签名更改为
extern "C" char* keygen ()
Otherwise it will be compiled with a C++ style name, and hence be a different function than the one declared in the header. 否则,它将使用C ++样式名称进行编译,因此它是与标头中声明的函数不同的函数。
I SOLVED THIS ISSUE BY MYSELF 我自己解决了这个问题
The problem was the linkage of the library. 问题是图书馆的联系。 I copied the libmywrapper.so(i renamed it) file to /usr/lib and linked with -mywrapper That's it :-)
我将libmywrapper.so(我将其重命名)文件复制到/ usr / lib并与-mywrapper链接就是这样:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.