【问题标题】:Linux RT Cross Compilation Code with CMake , undefined reference to `DAQmxCreateTask'带有 CMake 的 Linux RT 交叉编译代码,对 DAQmxCreateTask 的未定义引用
【发布时间】:2023-01-19 20:42:52
【问题描述】:

我试图交叉编译一种C代码与CMake里面操作系统使用VS代码. 但是由于将 (.so) 文件与项目链接时出错,我遇到了错误。我已经经历了很多解决方案,但未能运行该任务。 我的代码如下:

`     #include<stdio.h>
      #include"/home/admin/helloworld/src/NIDAQmx.h"

      TaskHandle taskHandle=0;
      int ret=0;

      void main()
      {
      printf("Hello world");
      ret=DAQmxCreateTask("task",&taskHandle);
      printf("Return for creating task is %d\n",ret);
      DAQmxStopTask (taskHandle);
      DAQmxClearTask(taskHandle);
      printf("Task closed ");

      }               `

运行任务时出错

`[ 50%] Linking C executable bin/helloWorld
 CMakeFiles/helloWorld.dir/home/admin/helloworld/src/helloWorld.c.o: In function `main':
 /home/admin/helloworld/src/helloWorld.c:11: undefined reference to `DAQmxCreateTask'
/home/admin/helloworld/src/helloWorld.c:13: undefined reference to `DAQmxStopTask'
/home/admin/helloworld/src/helloWorld.c:14: undefined reference to `DAQmxClearTask'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/helloWorld.dir/build.make:95: bin/helloWorld] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/helloWorld.dir/all] Error 2

 *  The terminal process "/bin/bash '-c', 'make'" failed to launch (exit code: 2). 
 *  Terminal will be reused by tasks, press any key to close it.  `

我修改了我的 CMakeLists.txt 如下:

`     cmake_minimum_required(VERSION 3.7.2)
      # project settings
      project(helloWorld VERSION 0.1.0)
      set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
      set(CMAKE_GENERATOR "Unix Makefiles")
      # executable settings
      add_executable(helloWorld ../src/helloWorld.c)
      set(CMAKE_BUILD_TYPE Debug)
      LINK_LIBRARIES(NIDAQmx ../src/libnidaqmx.so)
                             `

如果我删除与 NI DAQmx 代码相关的元素,则可以正常工作。

【问题讨论】:

    标签: c visual-studio-code cmake nidaqmx linux-rt


    【解决方案1】:

    命令 link_libraries 仅影响可执行文件和库,已添加它。来自documentation

    将库链接到以后添加的所有目标。

    所以你需要在该命令之后移动你的可执行文件:

    link_libraries(NIDAQmx ../src/libnidaqmx.so)
    ...
    add_executable(helloWorld ../src/helloWorld.c)
    

    此外,最好避免使用 link_libraries 命令,而使用 target_link_libraries 命令,您可以在其中明确指定要与给定库链接的目标:

    add_executable(helloWorld ../src/helloWorld.c)
    ...
    target_link_libraries(helloWorld NIDAQmx ../src/libnidaqmx.so)
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 2016-07-25
      • 2016-09-21
      相关资源
      最近更新 更多