这是我的 C++ 代码:
#include "stdlib.h"
#include "stdio.h"
#include <iostream>
int main(int argc, char *argv[] ) {
int width, height;
unsigned char *rgba;
FILE *fp = fopen("/home/pic.tif", "rb");
if(!fp)
std::cout<<"failed"<<std::endl;
rgba = floadtiff(fp, &width, &height);
fclose(fp);
if(rgba == 0)
printf("TIFF file unreadable\n");
}
我正在使用this @MalcolmMcLean 的库,这就是我的 loadtiff.c。我已经使用 gcc 编译了它,并尝试链接该库。
这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.12.2)
project (test)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} cmake/)
add_executable(test tiffs.cpp)
target_link_libraries(test loadtiff)
这些是我在尝试制作程序时遇到的错误:
error: ‘floadtiff’ was not declared in this scope
为什么我无法访问这个在 loadtiff.c 中定义的函数?
最佳答案
在tipps.cpp
中添加:
extern "C" {
#include "loadtiff.h"
}
在CMakeLists.txt
中更改为:
cmake_minimum_required(VERSION 2.8.12.2)
project(tiffs)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} cmake/)
add_library(loadtiff tiffloader/loadtiff.c)
target_include_directories(loadtiff PUBLIC tiffloader/loadtiff.h)
add_executable(tiffs tiffs.cpp)
target_link_libraries(tiffs loadtiff)
其中“tiffloader/”是放置“loadtiff”文件的位置。
不要以“test”等保留字命名项目或目标,否则您将收到 CMake 警告。
关于c++ - 包含 C 文件/与 CMake 的链接不适用于 C++ : cannot include function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44722714/