我在 .cpp
文件中修改了函数 foo
的主体,该文件是大型软件的一部分('large' 这样大量的 header 和实现从不同目录相互调用的文件)。该函数仍然采用相同的参数并返回相同的东西,因此在头文件中无需修改。
我在某处读到 C++ 的优点之一是它足够聪明,可以只重新编译需要的东西。
所以我(天真地?)做了 make all
,但后来我从调用该函数的源文件中得到了错误;错误都说 undefined reference to 'foo'
。
从上面的信息来看,我是否明显做错了什么?
好的,您要的是代码,这是之前的代码:
bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, Datum* datum) {
cv::Mat cv_img;
if (height > 0 && width > 0) {
cv::Mat cv_img_origin = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
cv::resize(cv_img_origin, cv_img, cv::Size(height, width));
} else {
cv_img = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
}
if (!cv_img.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return false;
}
datum->set_channels(3);
datum->set_height(cv_img.rows);
datum->set_width(cv_img.cols);
datum->set_label(label);
datum->clear_data();
datum->clear_float_data();
string* datum_string = datum->mutable_data();
for (int c = 0; c < 3; ++c) {
for (int h = 0; h < cv_img.rows; ++h) {
for (int w = 0; w < cv_img.cols; ++w) {
datum_string->push_back(
static_cast<char>(cv_img.at<cv::Vec3b>(h, w)[c]));
}
}
}
return true;
}
这是之后:
#include <unistd.h>
bool ReadImageToDatum(string& filename, const int label,
const int height, const int width, Datum* datum) {
cv::Mat cv_img;
/* dalyac addition to read/follow symlink if symlink*/
char buf[200];
if (readlink(filename.c_str(), buf, 200) > 0) {
filename = std::string(buf);
}
if (height > 0 && width > 0) {
cv::Mat cv_img_origin = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
cv::resize(cv_img_origin, cv_img, cv::Size(height, width));
} else {
cv_img = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
}
if (!cv_img.data) {
LOG(ERROR) << "Could not open or find file " << filename;
return false;
}
datum->set_channels(3);
datum->set_height(cv_img.rows);
datum->set_width(cv_img.cols);
datum->set_label(label);
datum->clear_data();
datum->clear_float_data();
string* datum_string = datum->mutable_data();
for (int c = 0; c < 3; ++c) {
for (int h = 0; h < cv_img.rows; ++h) {
for (int w = 0; w < cv_img.cols; ++w) {
datum_string->push_back(
static_cast<char>(cv_img.at<cv::Vec3b>(h, w)[c]));
}
}
}
return true;
}
我已经粘贴了Makefile在 pastebin 上,因为它有 400 行长。
感谢您的帮助!
最佳答案
这是不同之处。
1c1
< bool ReadImageToDatum(const string& filename, const int label,
---
> bool ReadImageToDatum(string& filename, const int label,
3a4,8
> /* dalyac addition to read/follow symlink if symlink*/
> char buf[200];
> if (readlink(filename.c_str(), buf, 200) > 0) {
> filename = std::string(buf);
> }
我会说将 const 添加到第一个参数是罪魁祸首。
您应该能够通过将常量添加到文件声明文件并重新编译...
关于C++:修改函数,尝试重新编译,得到对该函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24706361/