如何用pthread和lpq编译C代码

如何用pthread和lpq编译C代码,c,postgresql,ubuntu,pthreads,libpq,C,Postgresql,Ubuntu,Pthreads,Libpq,我一直在尝试编译一个非常简单的代码,其中包含postgres、pthread和mutex。现在,当我单独使用pthread时,一切都很好,当我单独使用postgres时也是如此!但是当我试图编译完整的代码时,我遇到了愚蠢的问题 gcc -o pleaseWork uploadData.c -I/usr/include/postgresql -lpthread -lpq 我得到: /tmp/ccitU5mG.o: In function `increaseQueryNum': uploadDa

我一直在尝试编译一个非常简单的代码,其中包含postgres、pthread和mutex。现在,当我单独使用pthread时,一切都很好,当我单独使用postgres时也是如此!但是当我试图编译完整的代码时,我遇到了愚蠢的问题

gcc -o pleaseWork uploadData.c -I/usr/include/postgresql -lpthread -lpq 
我得到:

/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
当我跑步时:

gcc -o omido  -I/usr/include/postgresql -lpq  -lpthread uploadData.c
我得到:

/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
我得到的最接近汇编的东西是(就错误数量而言):

其结果是:

/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
我不知道它是否有帮助,但下面是与-v相同的命令:

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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
pthread_mutex_t stackWaitMutex;
pthread_mutex_t stackMutex;
pthread_mutex_t counterMutex;
pthread_mutex_t stackPushMutex;
pthread_mutex_t stackPopMutex;
int done=0;
长槲皮素=0;
char*nameStack[500];
char*valueStack[500];
字符堆栈[100000][100000];
int stackCounter=0;
无效增量querynum(){
pthread_mutex_lock(&counterMutex);
queryNum++;
pthread_mutex_unlock(&counterMutex);
}
无效增量stacknum(){
pthread_mutex_lock(&stackMutex);
stackCounter++;
pthread_mutex_unlock(&stackMutex);
}
void decreaseStackNum(){
pthread_mutex_lock(&stackMutex);
堆栈计数器--;
pthread_mutex_unlock(&stackMutex);
}
无效推送(整数堆栈编号,字符*项){
strcpy(堆栈[堆栈计数器],项目);
增加stacknum();
}
字符*pop(整数堆栈编号){
char*ret;
//while(锁)
//usleep(2);
pthread_mutex_lock(&stackPopMutex);
decreaseStackNum();
ret=堆栈[堆栈计数器];
//lock=0;
pthread_mutex_unlock(&stackPopMutex);
返回ret;
}
无效*剂量操作(无效*参数){
而(1)
{
char*p=pop(0);
如果(p==NULL&&done){
返回;
}
}   
}
空*读卡器(空*参数){
int i;

对于(i=0;i,由于以下错误,此代码无法按原样为我构建:

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编译代码。有几个关键错误,例如不返回值的非void函数被用作pthread_create的输入。

第一个错误:

由于它会招致测试,我只是针对我脑海中立即出现的想法。我查看了头文件,识别出“联合”的“typedef”。因此,您必须提供该联合的适当类型。为了锁定所有互斥体,您必须指向任何有效数据

第二个错误:

必须提供“-lpq”。它指向库函数的地址。因此,重新安装开发库应该可以解决此问题

测试:

gcc-o请上传数据.c-lpthread-lpq


我目前正试图通过一个类似的命令来编译我的示例。它成功了。因此,这似乎是一项安装任务。

谢谢:)我没有想到这一点:/这是一个繁重的文本分析程序,这就是我需要那些巨大数组的原因。.现在我确实剪切了它们,正在编译:)我只是剪切了它们,或者以后可能会找到一个解决堆大小问题的方法。你认为堆大小会起作用吗?我还修复了返回的内容:)再次感谢。我建议调查gcc'-mcmodel'编译器标志。在运行时分配内存也可能起作用。
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