cygwin g ++链接器没有找到共享库 [英] cygwin g++ Linker doesn't find shared library

查看:226
本文介绍了cygwin g ++链接器没有找到共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在创建一个图书馆。当我将其编译为静态库时,它工作正常。现在我想把它变成一个共享库。库是在正确的位置创建的,但是当我尝试编译客户端代码时,链接阶段说它不能找到库。

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.

试图将其重命名为al或dylib,但这也没有帮助。当我把-v标志的链接,我可以看到,我的库路径有。我也试过不同的路径。我使用相对路径,但也有一个完整的路径,它没有找到它。

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.

Makefile形成库:

The Makefile form the library:

.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)

这里是应用程序的Makefile:

And here ist the Makefile for the application:

.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)


推荐答案

关于如何在cygwin下编译共享库。

After some experimenting I found a solution on how to compile a shared library under cygwin.

显然,编译器正在寻找一个DLL文件,即使它在cygwin内部。因此第一步是添加您的路径,其中库将转到PATH变量。

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.

 export PATH=$PATH:/cygdrive/d/src/c/lib

显然当链接到共享库时,链接器似乎默认查找一个DLL文件。我不知道为什么,因为在cygwin内,我期望它在其他UNIX系统上寻找一个.so文件。

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.

但是,有两个解决方案这两个工作。

However, there are two solutions to this, which both work.

首先,您可以使用名称.dll创建一个指向.so库的链接

First, you can create a link to your .so library with the name .dll

ln -s /cygdrive/d/src/lib/libsupport.so libsupport.dll

在这种情况下,makefile不需要改变,-lsupport会在链接时找到库。我更喜欢这个解决方案。

In this case the makefile doesn't have to be changed and -lsupport will find the library while linking. I prefer this solution.

其次,你可以指定链接器选项的全名。

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.

所以关键的事情似乎是共享库必须在PATH下cygwin。使用LD_LIBRARY_PATH在这种情况下没有帮助,因为你可以链接可执行文件,但是当尝试运行它时,它不会找到它。

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.

ldd nohupshd.exe

libsupport.so => not found

更新:由于某种原因,当我检查ldd时,我的库突然从列表。我发现cygwin使用名称来区分MS Windows和Unix共享库。所以为了使它工作,库的名称必须是cyg.so以使其工作,否则exectuable似乎是一些Windows构建。在这种情况下,您不需要创建名为x.dll的链接,因为共享库保留在Unix环境中。

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.

    $(LNK) -o cyg$(TARGET).so $(OBJ) -shared  



< eclipse进行调试,共享库的路径也必须在windows路径环境变量中。否则调试会话会立即终止,而不会出错。

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.

这篇关于cygwin g ++链接器没有找到共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆