6

ここで何が間違っているのかわかりません:

Q. 共有オブジェクトを使用してプログラムをコンパイルするときに、ライブラリが 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

これを行う理由は、別のプラットフォームでライブラリが作成されているためです。これにより、1 つのオブジェクトが呼び出されると機能しますが、2 番目のオブジェクトが構築されるとアプリがクラッシュします。私は簡単なテストとして上記をやっています!(面倒なことにそれほど単純ではありません)。

以下のソースファイル:

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
4

0 に答える 0