另一个;运行 cmake 命令后架构 x86_64 的未定义符号

问题描述

在我较小的 C 代码中包含一些共享库以调用库的一些函数,然后运行 ​​cmake -DCMAKE_BUILD_TYPE=Release ..cmake --build

我收到以下错误

Undefined symbols for architecture x86_64:
  "_textcat_Classify",referenced from:
      _tgcat_detect_language in tgcat.c.o
  "_textcat_Done",referenced from:
      _tgcat_detect_language in tgcat.c.o
  "_textcat_Init",referenced from:
      _tgcat_detect_language in tgcat.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)
make[2]: *** [libtgcat.dylib] Error 1
make[1]: *** [CMakeFiles/tgcat.dir/all] Error 2
make: *** [all] Error 2

我的头文件 tgcat.h 是这样的:

#ifndef TGCAT_H
#define TGCAT_H

/**
 * Library for determining topic and main languange of Telegram channels by
 * their recent content.
 */
 
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(_MSC_VER)
#  ifdef tgcat_EXPORTS
#    define TGCAT_EXPORT __declspec(dllexport)
#  else
#    define TGCAT_EXPORT __declspec(dllimport)
#  endif
#else
#  define TGCAT_EXPORT __attribute__((visibility("default")))
#endif

/**
 * Initializes the library. Must be called once before any other request.
 * \return 0 on success and a negative value on fail.
 */
TGCAT_EXPORT int tgcat_init();

/**
 * information about a Telegram channel.
 */
struct TelegramChannelInfo {
  /**
   * Title of the channel. A null-terminated string in UTF-8 encoding.
   */
  const char *title;

  /**
   * Description of the channel. A null-terminated string in UTF-8 encoding.
   */
  const char *description;

  /**
   * Number of available channel posts.
   */
  size_t post_count;

  /**
   * List of post_count channel posts. Posts are null-terminated strings in UTF-8 encoding.
   */
  const char **posts;
};

/**
 * Detects main language of channel posts.
 * \param[in] channel_info information about the channel.
 * \param[out] language_code Array to be filled with null-terminated ISO 639-1 language code
 *                           of the channel posts,or "other" if the language doesn�t have
 *                           a two-letter code.
 * \return 0 on success and a negative value on fail.
 */
TGCAT_EXPORT int tgcat_detect_language(const struct TelegramChannelInfo *channel_info,char language_code[6]);

/**
 * List of supported categories.
 */
enum TgcatCategory {
  TGCAT_CATEGORY_ART_AND_DESIGN,TGCAT_CATEGORY_BETS_AND_gambling,TGCAT_CATEGORY_TRAVEL_AND_TOURISM,TGCAT_CATEGORY_VIDEO_GAMES,TGCAT_CATEGORY_OTHER
};

const char *TGCAT_CATEGORY_NAME[] = {
  "Art & Design","Bets & gambling","Travel & Tourism","Video Games","Other"
};


TGCAT_EXPORT int tgcat_detect_category(const struct TelegramChannelInfo *channel_info,double category_probability[TGCAT_CATEGORY_OTHER + 1]);

#ifdef __cplusplus
}
#endif

#endif

这是我的 tgcat.c 文件,我在其中调用了共享库中的一些函数

#include "config.h"
#include "common.h"
#include "tgcat.h"
#include "textcat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int tgcat_init() {
  return 0;
}

int tgcat_detect_language(const struct TelegramChannelInfo *channel_info,char language_code[6]) {
  // choose a text with the longest length
  const char *text = channel_info->title;
  size_t text_len = strlen(channel_info->title);
  if (strlen(channel_info->description) > text_len) {
    text = channel_info->description;
    text_len = strlen(channel_info->description);
  }
  size_t i;
  for (i = 0; i < channel_info->post_count; i++) {
    if (strlen(channel_info->posts[i]) > text_len) {
      text = channel_info->posts[i];
      text_len = strlen(channel_info->posts[i]);
    }
  }
  void *h;
  char *result;
  h = textcat_Init("conf.txt");
  if (!h)
  {
    printf("Unable to init. Aborting.\n");
    exit(-1);
  }

  if (text_len != 0) {
    // guess language based on the first character
    // don't try this at home
    result = textcat_Classify(h,text,strlen(text) + 1);
    memcpy(language_code,result,10);
    textcat_Done(h);
    return 0;
    }
  }



int tgcat_detect_category(const struct TelegramChannelInfo *channel_info,double category_probability[TGCAT_CATEGORY_OTHER + 1]) {
  (void)channel_info;
  memset(category_probability,sizeof(double) * (TGCAT_CATEGORY_OTHER + 1));

  int i;
  for (i = 0; i < 10; i++) {
    category_probability[rand() % (TGCAT_CATEGORY_OTHER + 1)] += 0.1;
  }
  return 0;
}

而我的 CMakeLists.txt 是这样的:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(tgcat VERSION 1.0.0 LANGUAGES C)


find_library(LIBTEXTCAT_LIBRARY libtextcat PATHS "${CMAKE_CURRENT_SOURCE_DIR}" NO_DEFAULT_PATH)
add_library(tgcat SHARED tgcat.c)

我的工作目录中的文件层次如下 库猫/ libtextcat.dylib ... tgcat.h ... 以及上面 tgcat.c 模块中指示的其他头文件

我认为问题可能出在我的 CMakeLists.txt 文件上,该文件可能需要链接库,但我不知道如何实现。您的帮助将不胜感激

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...