c++ - 包含 C 文件/与 CMake 的链接不适用于 C++ : cannot include function

标签 c++ c cmake linker shared-libraries

这是我的 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/

相关文章:

c++ - C++数组下标差异

C++11 - 如何将 priority_queue 与共享指针 vector 一起使用?

c - 这段C代码如何解决?

c - 如何在字符串中搜索字符串? C

c++ - 错误 LNK1104 : cannot open file 'Debug\MyProjectLib.lib'

c - 使用 cmake find_package 在包中查找

cmake - 将列表转换为 cmake 函数的参数

c++ - C++ 中 main 之前发生了什么?

c++ - GoF 装饰器模式在 C++ 中使用静态多态性(模板)

c - 使 fgets 不打印由箭头键或其他控制键引起的 ^G 或 ^D 之类的东西