0

I'm more a c++ rookie. I want to create a dll using visa. I simplified the problem to an exe example. I've got the following MnWE:

#include <cstdlib>
#include <sstream>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdexcept>
#include <windows.h>
#include "visa.h"

using namespace std;

ViSession defaultRM = 0;

//opens VI-Session in specific address. Puts the defaultRM stuff "away".
void openVI (char* adress, ViSession vi, string mode, int timeout){
        if(defaultRM == 0){ 
            viOpenDefaultRM(&defaultRM);// Initialize VISA system
            if(defaultRM==0){
                cerr << "initalizing defaultRM failed";
            }else{
                cout << "defaultRM initalized";
            }
        }else{
        }
        ViAccessMode viMode = VI_NULL;
        ViUInt32 viTimeout = VI_NULL;
        if(mode == "EXCLUSIVE"){
            viMode = VI_EXCLUSIVE_LOCK;
            viTimeout = timeout;
        }else if(mode == "DEFAULT"){//Code für default kann hier eingefügt werden. Derzeit nichts vorgesehen.
        }else{
        }
        viOpen(defaultRM, adress, viMode, viTimeout, &vi);
    }


int main(){
    ViSession vi;
    openVI("ASRL2::INSTR", vi, "DEFAULT", 0);
    if(vi = 0){
        cout << "failed" << endl;
    }else{
        cout << "success" << endl;
    }


    return 0;
}

When compiling with gcc and the command

g++ -static -o VITest.exe VITest.cpp

I get the following error:

F:\Users\gabriel\AppData\Local\Temp\ccEx2dRK.o:VITest.cpp:(.text+0x17): undefined reference to `viOpenDefaultRM@4'
F:\Users\gabriel\AppData\Local\Temp\ccEx2dRK.o:VITest.cpp:(.text+0xbd): undefined reference to `viOpen@20'
collect2.exe: error: ld returned 1 exit status

As far as my gooleing brought me, it seems to have to do with the linking to the libs. I dont have the agilent-visa distribution on my pc yet. I just put the visa.h, visadef.h and visa.lib into the same folder as the cpp-file. I suppose, that it might have to with that. But I only want to install it, when definitely necessary. The final program will run on another machine.

MaestroGlanz
  • 321
  • 3
  • 12
  • 1
    I suggest that you test linking with `visa32.dll`, for example `g++ -Wall -O -L. -lvisa32 -Wl,--enable-stdcall-fixup -o test.exe test.cpp`. I tested this succesfully on linux with a cross compiler, a copy of `visa32.dll` was located in the same directory as `test.cpp` (your code). – J.J. Hakala Jun 09 '16 at 12:41

1 Answers1

2

You are not including the library into your compilation. Change your g++ line to:

g++ -static -o VITest.exe VITest.cpp -L. -lvisa
Smeeheey
  • 9,906
  • 23
  • 39
  • I tried that. But as far as I know, I would need the libvisa.a. But I do have the visa.lib. I just tried it and g++ says: `e:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../../mingw32/bin/ld.exe: cannot fin d -lvisa collect2.exe: error: ld returned 1 exit status` – MaestroGlanz Jun 09 '16 at 11:03
  • 1
    I presume you;re using MingGW, is that correct? Newer versions support `.lib` files, so it should work. Try changing `-l` to `-l:visa.lib` to specify it explicitly – Smeeheey Jun 09 '16 at 11:05
  • Okay, good to know. The MinGW is pretty new, so it should work. The apparent problem is, that it looks for the file in the MinGW folder and not in the current folder. – MaestroGlanz Jun 09 '16 at 11:07
  • Just add `-L.` to sort that problem, as per above edit – Smeeheey Jun 09 '16 at 11:08
  • Thanks a lot so far: But it still doesnt work. I assume, that it refers to a missing dll or lib. I still get this error message: `F:\Users\name\AppData\Local\Temp\ccdzAEIg.o:VITest.cpp:(.text+0x17): undefine d reference to `viOpenDefaultRM@4' F:\Users\name\AppData\Local\Temp\ccdzAEIg.o:VITest.cpp:(.text+0xbd): undefine d reference to `viOpen@20' collect2.exe: error: ld returned 1 exit status` – MaestroGlanz Jun 09 '16 at 11:12
  • I think you need the `visa.dll` file - it won't work without it. – Smeeheey Jun 09 '16 at 11:20