CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
CMake: How to link library A to library B, then link executable to library A
.
├── CMakeLists.txt
├── image.cpp
├── image.h
├── io_tools.h
├── libio_tools.so
└── main.cpp
- image.h 和 image.cpp 定义一个 Image 类
- 图像.cpp需要使用 libio_tools.so 和 io_tools.h 中的函数 是该库的头文件。
- main.cpp 是测试 Image 类。
我想将库 A(image.cpp( 链接到库 B(libio_tools.so(,然后将可执行文件(main.cpp(链接到库 A。
以下是我的CMakeLists.txt:
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)
构建它后,我收到了错误
[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
我认为CMakeLists有问题.txt。
以下是我的代码:
主.cpp:
#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;
}
图片.h:
#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
图片.cpp:
#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
io_tools.h:
#pragma once
#include <fstream>
#include <string>
#include <vector>
namespace igg {
namespace io_tools {
/// Dummy structure to store relevant image data.
struct ImageData {
int rows;
int cols;
int max_val;
std::vector<int> data;
};
/// Reads from a pgm image from ascii file. Returns empty ImageData if the path
/// is not found or any errors have occured while reading.
ImageData ReadFromPgm(const std::string& file_name);
/// Write image data into an ascii pgm file. Return true if successful.
bool WriteToPgm(const ImageData& image_data, const std::string& file_name);
} // namespace io_tools
} // namespace igg
我的朋友帮我找到了问题所在。CMakeLists.txt是正确的。问题出在图像.cpp。
图像.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';
}
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 使用gcc从静态链接的文件中查找可选符号
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 显示然后删除链接列表C
- 读取中的链接列表,然后按名称进行排序
- 为什么 clang++ 警告内联enable_if然后无法链接
- 分别创建目标文件,然后在Makefile中将它们链接在一起的目的是什么?< / h1 >
- c++链接自定义.so库,然后与JNI
- 升级到 win 10,然后我的 Direct3D11 项目无法链接