[libvirt] Compiling C++ program with libvirt

Hi everyone I am trying to compile a C++ program which uses libvirt to connect to an ESXi host. I got the code running if I wrote it in C, directly in the hellolibvirt.c example. However, I am now trying to compile that code in a C++ file which is not inside the example directory. I tried to compile with the following command: g++ -g -D debug=1 -c -llibvirt ESXWrapper.cpp g++ -g -D debug=1 -o ESXWrapper ESXWrapper.o The first line passes and creates ESXWrapper.o file. But when I run the second line, it gives me the following error: --- g++ -g -D debug=1 -c -llibvirt ESXWrapper.cpp g++ -g -D debug=1 -o ESXWrapper ESXWrapper.o ESXWrapper.o: In function `VPopCWrapper': /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:124: undefined reference to `virConnectOpenAuth' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:133: undefined reference to `virConnectGetURI' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:144: undefined reference to `virConnectClose' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:124: undefined reference to `virConnectOpenAuth' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:133: undefined reference to `virConnectGetURI' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:144: undefined reference to `virConnectClose' ESXWrapper.o: In function `showError': /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:186: undefined reference to `virConnCopyLastError' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:202: undefined reference to `virResetError' collect2: ld returned 1 exit status make: *** [all] Error 1 --- My makefile is the follwing: --- #vars CC=g++ OUTPUTFILE=ESXWrapper O_FILES=ESXWrapper.o FLAGS=-g -D debug=1 .PHONY: all all: esxwrapper $(CC) $(FLAGS) -o $(OUTPUTFILE) $(O_FILES) esxwrapper: ESXWrapper.cpp VPopCWrapper.h $(CC) $(FLAGS) -c -llibvirt ESXWrapper.cpp .PHONY: run run: all ./bignum .PHONY: clean clean: rm -f $(O_FILES) .PHONY: clean_all clean_all: clean rm -f $(OUTPUTFILE) --- Can anybody give me a hint, how to link correctly to libvirt. And also tell me, if there are special functions to use with C++, other than the examples provided in the source code, which are written in C. Thank you Adrian

2010/7/22 adrian wyssen <wyssenadrian@gmail.com>:
Hi everyone I am trying to compile a C++ program which uses libvirt to connect to an ESXi host. I got the code running if I wrote it in C, directly in the hellolibvirt.c example. However, I am now trying to compile that code in a C++ file which is not inside the example directory. I tried to compile with the following command: g++ -g -D debug=1 -c -llibvirt ESXWrapper.cpp g++ -g -D debug=1 -o ESXWrapper ESXWrapper.o The first line passes and creates ESXWrapper.o file. But when I run the second line, it gives me the following error: --- g++ -g -D debug=1 -c -llibvirt ESXWrapper.cpp
At this point g++ (actually ld) should have told you that it cannot find liblibvirt.so. when you pass a library to g++ using the -l you need to remove the lib prefix from the name, because it's implicit and ld will add it on its own. Passing -llibvirt will make ld look for the wrong file.
g++ -g -D debug=1 -o ESXWrapper ESXWrapper.o ESXWrapper.o: In function `VPopCWrapper': /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:124: undefined reference to `virConnectOpenAuth' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:133: undefined reference to `virConnectGetURI' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:144: undefined reference to `virConnectClose' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:124: undefined reference to `virConnectOpenAuth' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:133: undefined reference to `virConnectGetURI' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:144: undefined reference to `virConnectClose' ESXWrapper.o: In function `showError': /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:186: undefined reference to `virConnCopyLastError' /home/wyssen/school/Dropbox/virtualPOPC-1/NEW/vpopcwrapper/ESXWrapper.cpp:202: undefined reference to `virResetError' collect2: ld returned 1 exit status make: *** [all] Error 1 --- My makefile is the follwing: --- #vars CC=g++ OUTPUTFILE=ESXWrapper O_FILES=ESXWrapper.o FLAGS=-g -D debug=1
.PHONY: all all: esxwrapper $(CC) $(FLAGS) -o $(OUTPUTFILE) $(O_FILES) esxwrapper: ESXWrapper.cpp VPopCWrapper.h $(CC) $(FLAGS) -c -llibvirt ESXWrapper.cpp
$(CC) $(FLAGS) -c -lvirt ESXWrapper.cpp This should work better, assuming libvirt.so can be found in the common ld library path.
.PHONY: run run: all ./bignum
.PHONY: clean clean: rm -f $(O_FILES) .PHONY: clean_all clean_all: clean rm -f $(OUTPUTFILE) --- Can anybody give me a hint, how to link correctly to libvirt. And also tell me, if there are special functions to use with C++, other than the examples provided in the source code, which are written in C. Thank you Adrian
Just use the C API, nothing special for C++. Matthias
participants (2)
-
adrian wyssen
-
Matthias Bolte