2017-01-10 6 views
1

私は、postgres、pthread、およびmutexを含む非常に簡単なコードをコンパイルしようとしています。今私がpthreadだけでそれをするときは、すべてがうまくいきます。しかし、私が完全なコードをコンパイルしようとすると、私は愚かなprobemsに走っている!pthreadとlpqでCコードをコンパイルする方法

gcc -o pleaseWork uploadData.c -I/usr/include/postgresql -lpthread -lpq 

は私が取得:

gcc -o omido -I/usr/include/postgresql -lpq -lpthread uploadData.c 

私が手::

/tmp/ccitU5mG.o: In function `increaseQueryNum': 
uploadData.c:(.text+0x5): relocation truncated to fit: R_X86_64_32 against symbol `counterMutex' defined in COMMON section in /tmp/ccitU5mG.o 
uploadData.c:(.text+0x21): relocation truncated to fit: R_X86_64_32 against symbol `counterMutex' defined in COMMON section in /tmp/ccitU5mG.o 
/tmp/ccitU5mG.o: In function `increaseStackNum': 
uploadData.c:(.text+0x31): relocation truncated to fit: R_X86_64_32 against symbol `stackMutex' defined in COMMON section in /tmp/ccitU5mG.o 
uploadData.c:(.text+0x4a): relocation truncated to fit: R_X86_64_32 against symbol `stackMutex' defined in COMMON section in /tmp/ccitU5mG.o 
/tmp/ccitU5mG.o: In function `decreaseStackNum': 
uploadData.c:(.text+0x5a): relocation truncated to fit: R_X86_64_32 against symbol `stackMutex' defined in COMMON section in /tmp/ccitU5mG.o 
uploadData.c:(.text+0x73): relocation truncated to fit: R_X86_64_32 against symbol `stackMutex' defined in COMMON section in /tmp/ccitU5mG.o 
/tmp/ccitU5mG.o: In function `pop': 
uploadData.c:(.text+0xca): relocation truncated to fit: R_X86_64_32 against symbol `stackPopMutex' defined in COMMON section in /tmp/ccitU5mG.o 
uploadData.c:(.text+0xf7): relocation truncated to fit: R_X86_64_32 against symbol `stackPopMutex' defined in COMMON section in /tmp/ccitU5mG.o 
collect2: error: ld returned 1 exit status 

私が実行したときに、私は、コンパイルに持っている最も近いものが

/tmp/ccdXHrz3.o: In function `main': 
uploadData.c:(.text+0x5df): undefined reference to `PQconnectdb' 
uploadData.c:(.text+0x5f5): undefined reference to `PQstatus' 
uploadData.c:(.text+0x613): undefined reference to `PQfinish' 
uploadData.c:(.text+0x68c): undefined reference to `pthread_create' 
uploadData.c:(.text+0x6d5): undefined reference to `pthread_create' 
uploadData.c:(.text+0x6fe): undefined reference to `pthread_join' 
uploadData.c:(.text+0x727): undefined reference to `pthread_join' 
collect2: error: ld returned 1 exit status 

た(中エラー数の条件):

になり
gcc -o pleaseWork -I/usr/include/postgresql -pthread -lpthread -lpq uploadData.c 

:それは-vと同じコマンドができますが、ここだ場合、私は知らない

/tmp/ccg6RbUk.o: In function `main': 
uploadData.c:(.text+0x5df): undefined reference to `PQconnectdb' 
uploadData.c:(.text+0x5f5): undefined reference to `PQstatus' 
uploadData.c:(.text+0x613): undefined reference to `PQfinish' 
collect2: error: ld returned 1 exit status 

COLLECT_GCC_OPTIONS='-s' '-o' 'omido' '-I' '/usr/include/postgresql' '-pthread' '-v' '-mtune=generic' '-march=x86-64' 
as -v -I /usr/include/postgresql --64 -o /tmp/ccY3zbjH.o /tmp/ccdK2fuu.s 
GNU assembler version 2.24 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.24 
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/ 
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/ 
COLLECT_GCC_OPTIONS='-s' '-o' 'omido' '-I' '/usr/include/postgresql' '-pthread' '-v' '-mtune=generic' '-march=x86-64' 
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o omido -s /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. -lpq -lpthread /tmp/ccY3zbjH.o -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o 
/tmp/ccY3zbjH.o: In function `main': 
uploadData.c:(.text+0x5df): undefined reference to `PQconnectdb' 
uploadData.c:(.text+0x5f5): undefined reference to `PQstatus' 
uploadData.c:(.text+0x613): undefined reference to `PQfinish' 
collect2: error: ld returned 1 exit status 

をこれまで運が:(いずれかを行います体はここで何が間違っているか知っていますここ

は私のコードの簡易版である:私のためにそのまま起因するようなエラーのために、ビルドに失敗した

#include <pthread.h> 
#include <libpq-fe.h> 
#include <time.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 
pthread_mutex_t stackWaitMutex; 
pthread_mutex_t stackMutex; 
pthread_mutex_t counterMutex; 
pthread_mutex_t stackPushMutex; 
pthread_mutex_t stackPopMutex; 

int done=0; 
long queryNum=0; 

char *nameStack[500]; 
char *valueStack[500]; 
char stack[100000][100000]; 

int stackCounter=0; 

void increaseQueryNum(){ 
    pthread_mutex_lock(&counterMutex); 
    queryNum++; 
    pthread_mutex_unlock(&counterMutex); 
} 


void increaseStackNum(){ 
     pthread_mutex_lock(&stackMutex); 
     stackCounter++; 
     pthread_mutex_unlock(&stackMutex); 
} 
void decreaseStackNum(){ 
     pthread_mutex_lock(&stackMutex); 
     stackCounter--; 
     pthread_mutex_unlock(&stackMutex); 
} 


void push(int StackNumber, char* item){ 


    strcpy(stack[stackCounter ], item); 
    increaseStackNum(); 

} 

char* pop (int StackNumber){ 
    char *ret; 
    // while(lock) 
    // usleep(2); 
    pthread_mutex_lock(&stackPopMutex); 

    decreaseStackNum(); 

    ret = stack[stackCounter]; 
    // lock=0; 
    pthread_mutex_unlock(&stackPopMutex); 
    return ret; 
} 

void *doSeperation (void* args){ 

    while(1) 
    { 
     char *p = pop(0); 
     if(p==NULL && done){ 
      return; 
     } 

    } 
} 

void *reader (void *args){ 
    int i; 

    for(i = 0 ; i <12; i++){ 
     push(1, "test!"); 
    } 
    done=1; 
} 

int main(int argC , char** argV ){ 

    int i; 
    PGconn* connection = PQconnectdb("host=XXXXXX dbname=XXXXX user=postgres password=XXXX"); 

    if (PQstatus(connection) == CONNECTION_BAD) { 
     printf("Connection error\n"); 
     PQfinish(connection); 
     return -1; 
    } 
    printf("Connection ok\n"); 

    int maxThread =5; 
    int FilesCount =5; 

    pthread_t threads[1000] ; 

    pthread_t p2; 

    pthread_create(&p2,NULL,reader,&FilesCount); 

    usleep(60); 

    for(i = 0 ; i < maxThread; i++ ){ 
     pthread_create(&threads[i],NULL,doSeperation,NULL); 
    } 

    pthread_join(p2,NULL); 

    for( i = 0 ; i < maxThread; i ++){ 
     pthread_join(threads[i],NULL); 
    } 

    return 0; 
} 

答えて

1

このコード:

uploadData.c:25:(.text+0x5): relocation truncated to fit: R_X86_64_32 against symbol `counterMutex' defined in COMMON section in /tmp/ccfzQ53S.o 
uploadData.c:27:(.text+0x21): relocation truncated to fit: R_X86_64_32 against symbol `counterMutex' defined in COMMON section in /tmp/ccfzQ53S.o 

私は減らすことによってそれを解決しましたスタック配列を数オーダーだけ拡大する。その変更を行った後の例をリンクするのに問題はなかったので、リンカのエラーが関連する可能性があります。

コードを-Wallでコンパイルする必要があります。 pthread_createへの入力として使用されている、値を返さない非void関数など、いくつかの重大なバグがあります。

+0

ありがとう:)私はそれを考えなかった:それは重いテキスト解析プログラムなので、私はそれらの巨大な配列を必要としていた。今私はそれらをカットし、コンパイルしている:私はちょうどそれらをカットしたり、後でヒープサイズで作業すると、ヒープサイズが機能すると思いますか? また返品を修正しました:)もう一度ありがとうございます。 –

+0

gcc '-mcmodel'コンパイラフラグの調査をお勧めします。実行時にメモリを割り当てることもできます。 – parzival

関連する問題