0

I was trying to cross compile a C code with CMake inside Linux RT using VSCode. But I am encountering an error due to the mistake in linking (.so) file with project. I have gone through many solutions but failed to run the task. My code is given below:

  #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 ");

  } 

Error while running the task.

[ 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.
         

I modified my CMakeLists.txt as follows:

  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)
                         

If I remove the elements associated with NIDAQmx code working properly.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
RMR
  • 1
  • 1
  • In your [previous question](https://stackoverflow.com/questions/75171824/linux-rt-cross-compilation-code-with-cmake-undefined-reference-to-daqmxcreate/75172428?noredirect=1#comment132665432_75172428) I have told you that `LINK_LIBRARIES` should come **before** `add_executable`. Why do you repeat the same code again? – Tsyvarev Jan 23 '23 at 07:59
  • Hi, if we give `LINK_LIBRARIES` before `add_executable` , we cannot save the CMakelists.txt file.. – RMR Jan 23 '23 at 09:08

0 Answers0