g ++共享库找不到​​与-L相同的目录

时间:2012-06-13 11:26:37

标签: c++ linker g++ shared ld

我不知道我在这里做错了什么:

问。在使用共享对象编译程序时,为什么g ++找不到我的库?

我试图在我的c ++程序中包含一个共享库:

g++ -fpic -c sha.cpp
g++ -shared -o libsha.so sha.o
g++ main.cpp -o main -L. -lsha

其中sha.cpp和sha.h是库文件,main.cpp是我的程序。

我已尝试使用静态库,它可以找到:

g++ -static -c sha.cpp -o libsha.o
ar rcs libsha.a libsha.o
g++ main.cpp -o main -L. -lsha

平台是windows上的cygwin,这是输出:

rob@pc /cygdrive/c/src/a
$ g++ main.cpp -o shatest -L. -lsha
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lsha
collect2: ld returned 1 exit status

我已阅读所有论坛帖子,但该库位于同一个文件夹中!

$ ls
libsha.so  main.cpp  sha.cpp  sha.h  sha.o

我这样做的原因是,在另一个平台上,正在创建一个库,当一个对象被调用时,它可以工作,但是当构建第二个对象时应用程序崩溃。我正在做上面的简单测试! (烦恼不是那么简单)。

以下源文件:

的main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "sha.h"

void *thread_one( void *ptr );
void *thread_two( void *ptr );

main()
{
     pthread_t thread1, thread2;
     int  iret1, iret2;

     /* Create independent threads each of which will execute function */
     iret1 = pthread_create( &thread1, NULL, thread_one, 0);
     iret2 = pthread_create( &thread2, NULL, thread_two, 0);

     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */
     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);
     exit(0);
}

void *thread_one( void *ptr )
{
    printf("Run thread_one\n");
    CObj1 obj;
}

void *thread_two( void *ptr )
{
    printf("Run thread_two\n");
    CObj2 obj;
}

sha.cpp

#include <stdio.h>
#include <stdlib.h>

#include "sha.h"

CObj1::CObj1()
{
    printf("CObj1\n");
    a = 10;
    printf("CObj1: %d \n", a);
}

CObj2::CObj2()
{
    printf("CObj2\n");
    a = 10;
    printf("CObj2: %d \n", a);
}

sha.h

#ifndef LIB
#define LIB

class CObj1
{
public:
    CObj1();

private:
    int a;
};

class CObj2
{
public:
    CObj2();

private:
    int a;
};

#endif

0 个答案:

没有答案