Error LNK2019 when compiling TwinCAT ADS program

72 views Asked by At

I get error messages from Visual Studio 2022 linker when compiling. After looking through similar issues on this forum and trying as many solutions as I can find, I still can't resolve the problem.

I am building a test program to test TwinCAT 3 ADS communications from an external C++ program. The sample code I am testing is from here:

https://infosys.beckhoff.com/content/1033/tc3_adsdll2/124823307.html?id=7185691884721905562

This is the beginning of my code:

// Test program to read and write an ADS variable from/to TwinCAT

#include <iostream>
#include <windows.h>
#include <conio.h>


#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsApi.h"


int main()
{ 
    long nErr, nADSPort; 
    AmsAddr Addr; 
    PAmsAddr pAddr = &Addr; 
    ULONG lHdlVar, nData; 
    UINT nIntToRead;
    char szVar []={"MAIN.nCounter"}; 

    // Open communication port on the ADS router
    nADSPort = AdsPortOpen();
    nErr = AdsGetLocalAddress(pAddr);
    if (nErr) std::cerr << "Error: AdsGetLocalAddress: " << nErr << '\n';
    
    // TwinCAT 3 PLC1 port = 851
    pAddr->port = 851;

The required library 'TcAdsDll.lib' is installed and referenced as instructed here:

https://infosys.beckhoff.com/content/1033/tc3_adsdll2/123108747.html?id=7769290066237179923

For every call to the TwinCAT library I get an error:

Build started...
1>------ Build started: Project: ADSTest, Configuration: Debug Win32 ------
1>ADStest.obj : error LNK2019: unresolved external symbol _AdsPortOpen@0 referenced in function _main
1>ADStest.obj : error LNK2019: unresolved external symbol _AdsPortClose@0 referenced in function _main
1>ADStest.obj : error LNK2019: unresolved external symbol _AdsGetLocalAddress@4 referenced in function _main
1>ADStest.obj : error LNK2019: unresolved external symbol _AdsSyncWriteReq@20 referenced in function _main
1>ADStest.obj : error LNK2019: unresolved external symbol _AdsSyncReadReq@20 referenced in function _main
1>ADStest.obj : error LNK2019: unresolved external symbol _AdsSyncReadWriteReq@28 referenced in function _main
1>C:\Users\rainer.bars\Data\puppu\ADStest\Debug\ADSTest.exe : fatal error LNK1120: 6 unresolved externals
1>Done building project "ADSTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I am out of ideas. I suspect the cause is something fairly simple, but my skills are not sufficient to solve this.

Any help is greatly appreciated.

2

There are 2 answers

0
felipegf On

Make sure you are building the project for the same Configuration and Platform that you configured your linker input in the Project Properties.

Besides, you have to make sure your compiler is the same as the one used to compile the library (.lib) you are linking to.

0
Rainer Bärs On

Thank you for your help! Problem solved. The problem (apparently) was:

a) the library is 32-bit and I was trying to build a 64-bit solution

b) the library path that was referenced seemed to be faulty for some reason

Not sure what was the problem's root cause. Now I can't seem to replicate it.