使用个人C++库编译代码时,与头文件一起使用时会中断



我有一些C++实用程序函数,我一直在跨项目使用。 我想用这些实用程序创建一个库来为项目提供服务,而无需复制/粘贴我可能进行的任何更改。

我可以使用以下方法将单个.cpp文件转换为库:

$ g++ -c util.cpp
$ ar rcs libutil.a util.o

并制作了一个包含所有函数的 util.h 标头。

该库用于编译和运行一个简单的测试.cpp,该测试使用库函数打印一个点和一个向量的平均值:(我将标头移动到~/.local/include/,将库移动到~/.local/lib/(

$ g++ -o test test.cpp -L ~/.local/lib/ -lutil
$ ./test
.
4.5

但是,当我尝试使用库编译(部分(项目时,出现"{function} 未在此范围内声明"错误。

$ g++ -c source/linreg.cpp -L ~/.local/lib/ -lutil
...
linreg.cpp:11:18: error: ‘vecMean’ was not declared in this scope
...

试图重现这种行为,我写了这个:

// header.h
#ifndef HEADER_H
#define HEADER_H
void test();
#endif
// main.cpp
#include "header.h"
#include "util.h"
int main()
{
dot();
test();
return 0;
}
// test.cpp
#include <string>
#include <vector>
#include <iostream>
#include "util.h"
#include "header.h"
void test()
{
dot();
std::vector<double> x;
for(int i = 0; i < 10; ++i)
x.push_back(i * 1.0);
std::cout << vecMean(x) << std::endl;
}

哪个不编译。 根据 #includes 中的哪一个先于另一个, 抛出不同的错误。

上述抛出"'点'未在此范围内声明", 而下面的抛出"'测试'未在此范围内声明">

// main.cpp
#include "util.h"
#include "header.h"
...

这与我尝试编译实际项目时看到的行为相同。

如果我从main中删除dot((调用.cpp该示例可以编译并运行良好,除非将util.h include语句放在header.h语句之前(尽管我想util.h include毫无意义(。这导致未声明"测试"。

我觉得我错过了一些明显的东西, 尽管学习建立图书馆的整个过程一直很艰难。

看到头文件似乎是问题的一部分,我在下面添加我的 util.h, 以及利用率.cpp,用于良好的措施。

#ifndef HEADER_H
#define HEADER_H
#include <vector>
#include <tuple>
#include <fstream>
#include <string>
/***** utils *****/
// smallest/largest value from a vector
int indexSmallest(const std::vector<double> &vec);
int indexLargest(const std::vector<double> &vec);
// some vector operations
std::vector<double> sclMult(const std::vector<double> &vec, double scl);
std::vector<double> sclAdd(const std::vector<double> &vec, double scl);
std::vector<double> vecAdd(const std::vector<double> &vec1, const std::vector<double> &vec2);
std::vector<double> vecSub(const std::vector<double> &vec1, const std::vector<double> &vec2);
std::vector<std::vector<double> > vecCat(const std::vector<double> &vec1,
const std::vector<double> &vec2,
const std::vector<double> &vec3);
double vecMean(const std::vector<double> &vec);
double vecSum(const std::vector<double> &vec);
// sort two vectors of length 3 by the elements in the err vector
std::tuple<std::vector<std::vector<double> >, std::vector<double> >
sort(const std::vector<std::vector<double> > &X, const std::vector<double> &err);
// return maximum and minimum values from vector
std::vector<double> topbot(std::vector<double> &vec);
// print a dot
void dot(std::string str = ".");
// print a vector of doubles
void printVec(std::vector<double> vec);
// print a matrix of doubles
void printMat(std::vector<std::vector<double> > mat);
#endif
#include <vector>
#include <tuple>
#include <cmath>
#include <iostream>
#include <string>
#include "util.h"
int indexSmallest(const std::vector<double> &vec)
{
int index = 0;
for(int i = 1; i < vec.size(); i++)
{
if(vec[i] < vec[index])
index = i;
}
return index;
}
int indexLargest(const std::vector<double> &vec)
{
int index = 0;
for(int i = 1; i < vec.size(); i++)
{
if(vec[i] > vec[index])
index = i;
}
return index;
}
std::vector<double> sclMult(const std::vector<double> &vec, double scl)
{
std::vector<double> vvec(vec.size());
for(int i = 0; i < vec.size(); i++){
vvec[i] = vec[i] * scl;
}
//printVec(vvec);
return vvec;
}
std::vector<double> sclAdd(const std::vector<double> &vec, double scl)
{
std::vector<double> vvec(vec.size());
for(int i = 0; i < vec.size(); i++)
vvec[i] = vec[i] + scl;
return vvec;
}
std::vector<double> vecAdd(const std::vector<double> &vec1, const std::vector<double> &vec2)
{
std::vector<double> vvec(vec1.size());
//std::cout << "aaaa ";
//printVec(vec1);
for(int i = 0; i < vec1.size(); i++){
vvec[i] = (vec1[i] + vec2[i]);
}
return vvec;
}
std::vector<double> vecSub(const std::vector<double> &vec1, const std::vector<double> &vec2)
{
std::vector<double> vvec(vec1.size());
for(int i = 0; i < vec1.size(); i++)
vvec[i] = (vec1[i] - vec2[i]);
//vvec.push_back(vec1[i] - vec2[i]);
return vvec;
}
std::vector<std::vector<double> > vecCat(const std::vector<double> &vec1,
const std::vector<double> &vec2,
const std::vector<double> &vec3)
{
std::vector<std::vector<double> > vecCat(3);
vecCat[0] = vec1;
vecCat[1] = vec2;
vecCat[2] = vec3;
return vecCat;
}
std::tuple<std::vector<std::vector<double> >, std::vector<double> >
sort(const std::vector<std::vector<double> > &X, const std::vector<double> &err)
{
//std::cout << X.size() << ' ' << err.size() << std::endl;
std::vector<double> sortErr(3);
//std::vector<std::vector<double> > sortX;
int small = indexSmallest(err), large = indexLargest(err);
if(small == large)
return std::make_tuple(X,err);
int middle = fabs(small + large - 3);
//std::cout << small << ' ' << middle <<  ' ' << large << std::endl;
sortErr[0] = err[small];
sortErr[1] = err[middle];
sortErr[2] = err[large];
std::vector<std::vector<double> > sortX = vecCat(X[small],X[middle],X[large]);
/*  sortX[0] = X[small];
sortX[1] = X[middle];
sortX[2] = X[large];*/
return std::make_tuple(sortX,sortErr);
}
double vecMean(const std::vector<double> &vec)
{
double sum = 0;
for(int i = 0;i < vec.size();i++){
sum += vec[i];
}
return sum / vec.size();
}
double vecSum(const std::vector<double> &vec)
{
double sum = 0;
for(int i = 0;i < vec.size();i++){
sum += vec[i];
}
return sum;
}
void dot(std::string str)
{
std::cout << str << std::endl;
}
std::vector<double> topbot(std::vector<double> &vec)
{
double top = vec[0];
double bot = vec[0];
for(int i = 1; i < vec.size(); ++i){
if(vec[i] > top)
top = vec[i];
if(vec[i] < bot)
bot = vec[i];
}
std::vector<double> topbot = {top,bot};
return topbot;
}
void printVec(std::vector<double> vec)
{
for(int i = 0; i < vec.size(); ++i){
std::cout << vec[i] << ',';
}
std::cout << std::endl;
}
void printMat(std::vector<std::vector<double> > mat)
{
for(int i = 0; i < mat.size(); ++i){
printVec(mat[i]);
}
}
std::vector<double> head(std::vector<double> vec, int n)
{
std::vector<double> head;
for(int i = 0; i < n; ++i)
head.push_back(vec[i]);
return head;
}
std::vector<double> tail(std::vector<double> vec, int n)
{
std::vector<double> tail;
for(int i = vec.size() - n; i < vec.size(); ++i)
tail.push_back(vec[i]);
return tail;
}
std::vector<double> normalize(std::vector<double> vec)
{
std::vector<double> tb = topbot(vec);
std::vector<double> norm;
for(int i = 0; i < vec.size(); ++i)
norm.push_back((vec[i] - tb[1]) / (tb[0] - tb[1]));
return norm;
}
std::vector<double> vecLog(std::vector<double> vec)
{
std::vector<double> logged;
for(int i = 0; i < vec.size(); ++i)
logged.push_back(std::log(vec[i]));
return logged;
}
std::vector<double> vecExp(std::vector<double> vec)
{
std::vector<double> logged;
for(int i = 0; i < vec.size(); ++i)
logged.push_back(std::exp(vec[i]));
return logged;
}

问题是您在两个标头中都有相同的包含保护:

#ifndef HEADER_H
#define HEADER_H

因此,两个文件中的一个 - 这里,util.h- 被跳过,因为该符号实际上已经定义。

我还可以根据经验建议你给库起一些更独特的名字——比如说,verkutil——并有一个包含保护来匹配?我的经验是,太多的项目都有自己的UTIL_H符号和类似名称的文件。

最新更新