I have been creating a library. When I compile it as a static library, it works fine. Now I wanted to turn it into a shared library. The library is created and in the proper place, but when I try to compile the client code, the linking phase says that it cant't find the library.
我一直在创建一个库。当我将它编译为静态库时,它工作得很好。现在我想把它变成一个共享的库。库是在适当的位置创建的,但是当我试图编译客户端代码时,链接阶段说它找不到库。
I already tried to rename it to al or dylib but that doesn't help either. When I put the -v flag on the linking, I can see that my library path is there. I also tried different paths. I use a relative path, but also with a full path it doesn't find it.
我已经尝试将它重命名为al或dylib,但这也没有帮助。当我把-v标记放在链接上时,我可以看到我的库路径在那里。我也尝试了不同的道路。我使用一个相对路径,但它也没有找到完整路径。
The Makefile form the library:
Makefile来自库:
.SUFFIXES:
.SUFFIXES: .o .cpp
.SUFFIXES: .o .d
CC := g++
LNK:= g++
CXXFLAGS_RELEASE = -fPIC -shared -O2 -Wall -fmessage-length=0
CXXFLAGS_DEBUG = -fPIC -shared -g -Wall -fmessage-length=0 -D _DEBUG
CXXFLAGS = $(CXXFLAGS_DEBUG)
OBJDIR:= obj
SRCDIR:= src
HDIR:= include
INCLUDE_PATHS:= -Iinclude -Iinclude/interfaces -Iinclude/support
CPP_FILES := propertyfile/propertyfile.cpp \
propertyfile/propertyitem.cpp \
propertyfile/propertyfactory.cpp \
helper/string_helper.cpp
OBJ := $(patsubst %.cpp,$(OBJDIR)/%.o, $(CPP_FILES))
SRC := $(patsubst %.cpp,$(SRCDIR)/%.o, $(CPP_FILES))
LIBS:=
TARGET:= libsupport.so
all: $(TARGET)
$(TARGET): $(OBJ)
$(LNK) -o $(TARGET) $(OBJ) -shared
@cp $(TARGET) ../lib
@cp -r include ..
clean:
rm -f $(OBJ) $(ASM) $(TARGET)
-include $(patsubst %.cpp,$(OBJDIR)/%.d, $(CPP_FILES))
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(OBJDIR)/%.d
@mkdir -p `dirname $@`
$(CC) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATHS)
$(OBJDIR)/%.d: $(SRCDIR)/%.cpp
@mkdir -p `dirname $@`
$(CC) $(CXXFLAGS) -MM -MT $@ -MF $(OBJDIR)/$*.d -c $< $(INCLUDE_PATHS)
And here ist the Makefile for the application:
这里是申请的Makefile:
.SUFFIXES:
.SUFFIXES: .o .cpp
CC := g++
LD := g++
CXXFLAGS_RELEASE = -O2 -Wall -fmessage-length=0
CXXFLAGS_DEBUG = -g -Wall -fmessage-length=0 -D _DEBUG
CXXFLAGS = $(CXXFLAGS_DEBUG)
OBJDIR:= obj
SRCDIR:= src
INCLUDE_PATHS:= -Iinclude -I../include
LIBS:= -L /cygdrive/d/src/c/lib -lsupport
CPP_FILES := nohupshd.cpp \
daemon.cpp \
task.cpp
OBJ := $(patsubst %.cpp,$(OBJDIR)/%.o, $(CPP_FILES))
SRC := $(patsubst %.cpp,$(SRCDIR)/%.o, $(CPP_FILES))
TARGET:= nohupshd
all: $(TARGET)
$(TARGET): $(OBJ)
$(LD) -o $(TARGET) $(OBJ) $(LIBS)
clean:
rm -f $(OBJ) $(ASM) $(TARGET)
-include $(patsubst %.cpp,$(OBJDIR)/%.d, $(CPP_FILES))
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(OBJDIR)/%.d
@mkdir -p `dirname $@`
$(CC) $(CXXFLAGS) -c $< -o $@ $(INCLUDE_PATHS)
$(OBJDIR)/%.d: $(SRCDIR)/%.cpp
@mkdir -p `dirname $@`
$(CC) $(CXXFLAGS) -MM -MT $@ -MF $(OBJDIR)/$*.d -c $< $(INCLUDE_PATHS)
1 个解决方案
#1
12
After some experimenting I found a solution on how to compile a shared library under cygwin.
经过一些实验,我找到了一个如何在cygwin下编译共享库的解决方案。
Apparently the compiler is looking for a DLL file even though it is inside cygwin. so the first step is to add your path, where the library is going to be to the PATH variable.
显然,编译器正在寻找一个DLL文件,即使它在cygwin中。因此,第一步是添加路径,库将在路径变量中。
export PATH=$PATH:/cygdrive/d/src/c/lib
Apparently when linking against a shared library, the linker seems to look for a DLL file by default. I don't know why, because inside cygwin I would expect it to look for a .so file just like on other UNIX systems.
显然,当链接到共享库时,链接器默认会查找一个DLL文件。我不知道为什么,因为在cygwin中,我希望它像在其他UNIX系统上一样查找.so文件。
However, there are two solutions to this, which both work.
然而,有两种解决方法,都是有效的。
First, you can create a link to your .so library with the name .dll
首先,您可以创建一个链接到您的so库。
ln -s /cygdrive/d/src/lib/libsupport.so libsupport.dll
In this case the makefile doesn't have to be changed and -lsupport will find the library while linking. I prefer this solution.
在这种情况下,不必修改makefile,并且-lsupport将在链接时找到库。我喜欢这个解决方案。
Second, you can specify the linker option with the full name.
其次,可以使用全名指定链接器选项。
LIBS:= -L /cygdrive/d/src/c/lib -l:libsupport.so
then you don't have to create a link.
然后你不必创建一个链接。
So the crucial thing seems to be that the shared library must be in the PATH under cygwin. Using LD_LIBRARY_PATH doesn't help in that case as you can link the executable, but when trying to run it, it will not find it.
因此,关键的事情似乎是共享库必须位于cygwin的路径中。在这种情况下,使用LD_LIBRARY_PATH没有帮助,因为您可以链接可执行文件,但是当尝试运行它时,它将不会找到它。
ldd nohupshd.exe
libsupport.so => not found
UPDATE: For some reason when I checked with ldd, my library was suddenly gone from the list. I found out that cygwin uses the name to differentiate between MS Windows and Unix shared libraries. So in order to make it work, the name of the library must be cyg.so to make it work, otherwise the exectuable seems to be some Windows build. In this case you don't need to create the link named x.dll as the shared library stays inside the Unix environment.
更新:由于某些原因,当我检查ldd时,我的库突然从列表中消失了。我发现cygwin使用这个名称来区分Windows和Unix共享库。所以为了使它工作,库的名字必须是cyg。所以要让它工作,否则可恶的似乎是一些Windows构建。在这种情况下,不需要创建名为x的链接。作为共享库的dll保存在Unix环境中。
$(LNK) -o cyg$(TARGET).so $(OBJ) -shared
When using eclipse for debugging, the path to the shared library must also be in the windows path environment variable. Otherwise the debug session immediately terminates without an error.
在使用eclipse进行调试时,共享库的路径也必须位于windows path环境变量中。否则,调试会话立即终止,不会出现错误。
#1
12
After some experimenting I found a solution on how to compile a shared library under cygwin.
经过一些实验,我找到了一个如何在cygwin下编译共享库的解决方案。
Apparently the compiler is looking for a DLL file even though it is inside cygwin. so the first step is to add your path, where the library is going to be to the PATH variable.
显然,编译器正在寻找一个DLL文件,即使它在cygwin中。因此,第一步是添加路径,库将在路径变量中。
export PATH=$PATH:/cygdrive/d/src/c/lib
Apparently when linking against a shared library, the linker seems to look for a DLL file by default. I don't know why, because inside cygwin I would expect it to look for a .so file just like on other UNIX systems.
显然,当链接到共享库时,链接器默认会查找一个DLL文件。我不知道为什么,因为在cygwin中,我希望它像在其他UNIX系统上一样查找.so文件。
However, there are two solutions to this, which both work.
然而,有两种解决方法,都是有效的。
First, you can create a link to your .so library with the name .dll
首先,您可以创建一个链接到您的so库。
ln -s /cygdrive/d/src/lib/libsupport.so libsupport.dll
In this case the makefile doesn't have to be changed and -lsupport will find the library while linking. I prefer this solution.
在这种情况下,不必修改makefile,并且-lsupport将在链接时找到库。我喜欢这个解决方案。
Second, you can specify the linker option with the full name.
其次,可以使用全名指定链接器选项。
LIBS:= -L /cygdrive/d/src/c/lib -l:libsupport.so
then you don't have to create a link.
然后你不必创建一个链接。
So the crucial thing seems to be that the shared library must be in the PATH under cygwin. Using LD_LIBRARY_PATH doesn't help in that case as you can link the executable, but when trying to run it, it will not find it.
因此,关键的事情似乎是共享库必须位于cygwin的路径中。在这种情况下,使用LD_LIBRARY_PATH没有帮助,因为您可以链接可执行文件,但是当尝试运行它时,它将不会找到它。
ldd nohupshd.exe
libsupport.so => not found
UPDATE: For some reason when I checked with ldd, my library was suddenly gone from the list. I found out that cygwin uses the name to differentiate between MS Windows and Unix shared libraries. So in order to make it work, the name of the library must be cyg.so to make it work, otherwise the exectuable seems to be some Windows build. In this case you don't need to create the link named x.dll as the shared library stays inside the Unix environment.
更新:由于某些原因,当我检查ldd时,我的库突然从列表中消失了。我发现cygwin使用这个名称来区分Windows和Unix共享库。所以为了使它工作,库的名字必须是cyg。所以要让它工作,否则可恶的似乎是一些Windows构建。在这种情况下,不需要创建名为x的链接。作为共享库的dll保存在Unix环境中。
$(LNK) -o cyg$(TARGET).so $(OBJ) -shared
When using eclipse for debugging, the path to the shared library must also be in the windows path environment variable. Otherwise the debug session immediately terminates without an error.
在使用eclipse进行调试时,共享库的路径也必须位于windows path环境变量中。否则,调试会话立即终止,不会出现错误。