3

我有一个编译基本错误。

简而言之,我的主要内容是:

主文件

#include "sendArrayObj.h"
sendArrayObj* sendqueue;

void foo(){
    int i = sendqueue->count()
}

int main(){
    int i =10;
    sendqueue = new sendArrayObj(i);
    foo();
}

发送数组对象.h

using namespace std;
#include <string.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>


class sendArrayObj{
private:
    int size,toEnqueue,toDequeue,lastDequeuedInd;
    unsigned long long totalEnqueued,totalDequeued;

public:
    char** arr;
    int* lengths;
    sendArrayObj(int size);
    void enqueue(string str);
    char* dequeue();
    int count();
    int getDequeuedSize();

};

sendArrayObj.cpp

#include "sendArrayObj.h"

pthread_mutex_t mutex_requestsqueue = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_count = PTHREAD_MUTEX_INITIALIZER;


sendArrayObj::sendArrayObj( int size){
this->size = size;
toEnqueue = 0;
toDequeue = 0;
totalEnqueued = 0;
totalDequeued = 0;
lastDequeuedInd = -1;
arr = (char**)malloc(size*sizeof(char*));//alloc number of pointers
lengths = (int*)malloc(size*sizeof(int));//alloc number of lengths
}

void sendArrayObj::enqueue(std::string str){
int strSize = str.size();
pthread_mutex_lock( &mutex_requestsqueue );
if (arr[toEnqueue]!= NULL){
    free(arr[toEnqueue]);
    toDequeue = (toDequeue+1)%size;
    totalDequeued++;
}
arr[toEnqueue] = (char*)malloc(strSize);
memcpy(arr[toEnqueue],(char*)str.c_str(),strSize);
lengths[toEnqueue] = strSize;
toEnqueue = (toEnqueue+1)%size;
totalEnqueued++;
pthread_mutex_unlock( &mutex_requestsqueue );
}



char* sendArrayObj::dequeue(){
pthread_mutex_lock( &mutex_requestsqueue );
//allocate memory to the ans 
//char* ans = new char[lengths[toDequeue]];
char* ans = arr[toDequeue];
lastDequeuedInd = lengths[toDequeue];
//memcpy(ans,arr[toDequeue],lengths[toDequeue]);
//if (arr[toDequeue]!=NULL){
    //delete[] arr[toDequeue];
arr[toDequeue] = NULL;
//}
toDequeue = (toDequeue+1)%size;
totalDequeued++;
//printf("totalDequeued is %d\n",totalDequeued);
pthread_mutex_unlock( &mutex_requestsqueue );
return ans;

}



int sendArrayObj::count(){
pthread_mutex_lock( &mutex_count );
int ans = totalEnqueued-totalDequeued; 
pthread_mutex_unlock( &mutex_count );
//printf("count is %d\n",ans);
return ans;
}

int sendArrayObj::getDequeuedSize(){    
return lastDequeuedInd;
}

我的编译批次是

g++ -g -pthread utils/hregex.cpp ExcludeFields.cpp utils/sha1.cpp utils/utils.cpp utils/base64.cpp utils/xmlhelper.cpp utils/messagehelper.cpp utils/safe_queue.cpp utils/parser.cpp utils/default_config.cpp ExcludesParameters.cpp main_process_helper.cpp main.cpp -Iutils -Ibusiness_objects -o telepath_sniff

并且文件在文件夹“utils”中

知道为什么我会得到它吗?

谢谢

4

2 回答 2

4

您必须告诉链接器要将哪些目标文件链接到您的应用程序中。

编辑:

您发布了以下命令,并由我进行了可读性调整:

g++ -g -pthread 
utils/hregex.cpp \
ExcludeFields.cpp \
utils/sha1.cpp \
utils/utils.cpp \
utils/base64.cpp \
utils/xmlhelper.cpp \
utils/messagehelper.cpp \
utils/safe_queue.cpp \
utils/parser.cpp \
utils/default_config.cpp \
ExcludesParameters.cpp \
main_process_helper.cpp \
main.cpp -Iutils -Ibusiness_objects -o telepath_sniff

使用 GNU 工具,您可以将依赖项放在依赖项之后

例如,如果main.cpp取决于foobar.cpp,你写

g++ main.cpp foobar.cpp

因为链接器会尝试链接main.cpp,然后保留一个稍后需要解决的引用列表。你订错了。

最后,你完全失去sendArrayObj.cpp了。

所以,

  • 把所依赖的放在第一位,把它依赖的放在后面
  • 添加sendArrayObj.cpp到您的构建脚本命令

旁注:您通常应该

  • 使用包括警卫
  • 避免全局变量
  • 避免手动内存管理
  • 不暴露可写成员变量
  • 要么很好地定义复制语义,要么一起禁止复制
  • 不使用 C 头文件(例如 use cstdio, not stdio.h
  • 如果成员函数const不应该改变类实例的可观察状态,则创建成员函数
  • 不放入using namespace头文件
  • 喜欢标准库容器,例如动态分配的数组std::liststd::vector超过动态分配的数组
  • 为管理内存的类编写析构函数
  • 释放记忆
  • 使用一致的缩进方案
  • 不包括您不需要的标题,这会花费您的时间和金钱
  • 使用构建系统(例如 makefile、IDE 项目文件、cmake、Scons)
  • 获得有关您是新手的主题的良好介绍性文献
  • 如果你没有读过好的介绍性文献,就不要写那么大的项目,这对 C++来说是双重的
于 2012-12-19T15:01:02.077 回答
1

您的编译命令行似乎sendArrayObj.cpp缺少它。添加它应该可以解决这个特定问题;但是,考虑分别为不同的编译单元构建目标文件以加快后续编译(您只需重新编译那些更改的编译单元)。

于 2012-12-19T15:12:29.090 回答