dnull = open(os.devnull, 'w')
proc = subprocess.Popen('ll'.split(), stdout=dnull, stderr=subprocess.PIPE)
for i in proc.stderr:
print(i)
# check for error message strings and do something with them
我想将库A(image.cpp)链接到库B(libio_tools.so),然后将可执行文件(main.cpp)链接到库A。
下面是我的CMakeLists.txt:
.
├── CMakeLists.txt
├── image.cpp
├── image.h
├── io_tools.h
├── libio_tools.so
└── main.cpp
构建之后,我得到了错误
cmake_minimum_required( VERSION 2.8 )
set (CMAKE_CXX_STANDARD 11)
set( CMAKE_BUILD_TYPE "Debug" )
project(debug)
find_library(IO_TOOLS io_tools "${PROJECT_SOURCE_DIR}")
if(NOT IO_TOOLS)
message(FATAL_ERROR "iotools library not found" )
endif()
add_library(image image.cpp)
target_link_libraries(image ${IO_TOOLS})
add_executable(bin_main main.cpp)
target_link_libraries(bin_main image)
我认为CMakeLists.txt出了问题。
下面是我的代码:
main.cpp:
[cmake] Configuring done
[cmake] Generating done
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /home/lm/debug/build --config Debug --target all -- -j 6
[build] [ 50%] Built target image
[build] [ 75%] Linking CXX executable bin_main
[build] CMakeFiles/bin_main.dir/main.cpp.o: In function `main':
[build] /home/lm/debug/main.cpp:4: undefined reference to `igg::Image::Image(int, int)'
[build] /home/lm/debug/main.cpp:6: undefined reference to `igg::Image::at(int, int)'
[build] /home/lm/debug/main.cpp:7: undefined reference to `igg::Image::WriteToPgm(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[build] collect2: error: ld returned 1 exit status
[build] make[2]: *** [bin_main] Error 1
[build] make[1]: *** [CMakeFiles/bin_main.dir/all] Error 2
[build] CMakeFiles/bin_main.dir/build.make:85: recipe for target 'bin_main' failed
[build] CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/bin_main.dir/all' failed
[build] Makefile:83: recipe for target 'all' failed
[build] make: *** [all] Error 2
[build] Build finished with exit code 2
image.h:
#include "image.h"
int main(int argc, char const *argv[]) {
igg::Image test(100, 100);
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++) test.at(i, j) = 255;
test.WriteToPgm("test.pgm");
return 0;
}
image.cpp:
#pragma once
#include <string>
#include <vector>
namespace igg {
class Image {
public:
///////////////////// Create the public interface here ///////////////////////
Image();
Image(int rows, int cols);
int rows() const; // const function
int cols() const;
int& at(int rows, int cols);
bool FillFromPgm(const std::string& file_name);
void WriteToPgm(const std::string& file_name);
private:
int rows_ = 0;
int cols_ = 0;
int max_val_ = 255;
std::vector<int> data_;
};
} // namespace igg
io_tools.h:
#include <iostream>
#include <string>
#include <vector>
#include "io_tools.h"
namespace igg {
class Image {
public:
///////////////////// Create the public interface here ///////////////////////
Image() : rows_(10), cols_(10), data_(std::vector<int>(100, 0)) {}
Image(int rows, int cols)
: rows_(rows), cols_(cols), data_(std::vector<int>(rows * cols, 0)) {}
int rows() const { return rows_; } // const function
int cols() const { return cols_; }
int& at(int rows, int cols) { return data_[rows * rows_ + cols]; }
bool FillFromPgm(const std::string& file_name) {
igg::io_tools::ImageData tmp = igg::io_tools::ReadFromPgm(file_name);
if (tmp.cols == 0) return false;
rows_ = tmp.rows;
cols_ = tmp.cols;
data_ = tmp.data;
return true;
}
void WriteToPgm(const std::string& file_name) {
igg::io_tools::ImageData image_data = {rows_, cols_, max_val_, data_};
if (igg::io_tools::WriteToPgm(image_data, file_name))
std::cout << "write " + file_name + " successfully!" << '\n';
else
std::cout << "write not successful!" << '\n';
}
private:
int rows_ = 0;
int cols_ = 0;
int max_val_ = 255;
std::vector<int> data_;
};
} // namespace igg
答案 0 :(得分:0)
我的朋友帮助我找到了问题。 CMakeLists.txt是正确的。问题出在image.cpp中。
image.cpp应该是:
#include "image.h"
#include <iostream>
#include <string>
#include <vector>
#include "io_tools.h"
namespace igg{
Image::Image() : rows_(10), cols_(10), data_(std::vector<int>(100, 0)) {}
Image::Image(int rows, int cols)
: rows_(rows), cols_(cols), data_(std::vector<int>(rows * cols, 0)) {}
int Image::rows() const { return rows_; } // const function
int Image::cols() const { return cols_; }
int& Image::at(int rows, int cols) { return data_[rows * rows_ + cols]; }
bool Image::FillFromPgm(const std::string& file_name) {
igg::io_tools::ImageData tmp = igg::io_tools::ReadFromPgm(file_name);
if (tmp.cols == 0) return false;
rows_ = tmp.rows;
cols_ = tmp.cols;
data_ = tmp.data;
return true;
}
void Image::WriteToPgm(const std::string& file_name) {
igg::io_tools::ImageData image_data = {rows_, cols_, max_val_, data_};
if (igg::io_tools::WriteToPgm(image_data, file_name))
std::cout << "write " + file_name + " successfully!" << '\n';
else
std::cout << "write not successful!" << '\n';
}
}