Unable to link .lib to VSCode-Undefined reference to `DAQmxCreateTask'

114 Views Asked by At

Building a C Code using Visual code studio having a third-party Library file from National Instruments. I have already included header file "NIDAQmx.h" to the code. The library file is still not linked with my code. Please find my code below:

`#include<stdio.h>
#include"C:\Program Files (x86)\National 
Instruments\Shared\ExternalCompilerSupport\C\include\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 ");

}`

tasks.json file task

given below:

`{
"tasks": [
    {
        "type": "shell",
        "label": "C/C++: gcc.exe build active file",
        "command": "C:\\MinGW\\bin\\gcc.exe",
        "args": [
            "-g",
            "test.c",
            "-o",
            "test.exe",
            "-LC:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib32\\msvc",
            "-lNIDAQmx"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"
}`

When I am running the C Code, I got the Consol Output as follows:

**[Running] cd "c:\Users\User\Documents\my_c\src\" && gcc test.c -o test 
 && "c:\Users\User\Documents\my_c\src\"test
 c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64- 
 w64-mingw32/bin/ld.exe: 
 C:\Users\User\AppData\Local\Temp\ccKqDitK.o:test.c:(.text+0x82): 
 undefined reference to `DAQmxCreateTask'
 c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64- 
 w64-mingw32/bin/ld.exe: 
 C:\Users\User\AppData\Local\Temp\ccKqDitK.o:test.c:(.text+0xae): 
 undefined reference to `DAQmxStopTask'
 c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64- 
 w64-mingw32/bin/ld.exe: 
 C:\Users\User\AppData\Local\Temp\ccKqDitK.o:test.c:(.text+0xbd): 
 undefined reference to `DAQmxClearTask'
 collect2.exe: error: ld returned 1 exit status

 [Done] exited with code=1 in 0.181 seconds**

as suggested by one of the forums i tried a command(mentioned below)specifying file path in the terminal without any error:

 'PS D:\VSCODES> gcc test.c -o test -L"C:\Program Files (x86)\National 
  Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc" -lNIDAQmx'

While debug C/C++ File, I get the following message (VSCode Terminal):

    '***  Executing task: C:\MinGW\bin\gcc.exe -g test.c -o test.exe '- 
     LC:\Program Files (x86)\National 
     Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc' -lNIDAQmx 
     c:/mingw/bin/../lib/gcc/x86_64-w64- 
     mingw32/11.2.0/../../../../x86_64- 
     w64-mingw32/bin/ld.exe: skipping incompatible C:\Program Files 
     (x86)\National 
     Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc/NIDAQmx.lib 
     when searching for -lNIDAQmx
     c:/mingw/bin/../lib/gcc/x86_64-w64- 
     mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping 
     incompatible C:\Program Files (x86)\National 
     Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc/NIDAQmx.lib 
     when searching for -lNIDAQmx
     c:/mingw/bin/../lib/gcc/x86_64-w64- 
     mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping 
     incompatible C:\Program Files (x86)\National 
     Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc\NIDAQmx.lib 
     when searching for -lNIDAQmx
     c:/mingw/bin/../lib/gcc/x86_64-w64- 
     mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot 
     find -lNIDAQmx
     c:/mingw/bin/../lib/gcc/x86_64-w64- 
     mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping 
     incompatible C:\Program Files (x86)\National 
     Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc/NIDAQmx.lib 
     when searching for -lNIDAQmx
     collect2.exe: error: ld returned 1 exit status**'
1

There are 1 best solutions below

2
On

Edit the tasks,json file as follows:

 {
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\MinGW\\bin\\gcc.exe",
            "args": [
                "-g",
                "test.c",
                "-o",
                "test.exe",
                "-LC:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib64\\msvc",
                "-lNIDAQmx"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
    }