Visual C++ : XGBoost does not work when called from a DLL - c++

I have a requirement to use XGBoost within a Visual C++ project DLL.
I have been able to download and build the XGBoost source using VC++ and CMake.
When I include the XGBoost code in a test console application, it works fine.
However, when I try to replicate what I've done with the console application in a DLL project, it won't compile.
I am struggling to even use a basic XGBoost type within the project.
I suspect the problem is my ignorance with DLL projects and would appreciate your help.
Here's what's happening in my DLL project:
When I use the following include as the very first line in a cpp class file, it compiles:
#include <xgboost\c_api.h>
With it compiling, if I try to use a simple type defined in this include file, the build fails with the following message:
...\dll_test\xgb_driver.cpp(20): error C2065: 'BoosterHandle': undeclared identifier
This is the line that causes the error:
BoosterHandle my_handle;
"BoosterHandle" is in fact defined in <xgboost\c_api.h>
When I put the include below any other include, I get the following error messages:
1>c:\tools\src\xgboost\include\xgboost\c_api.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\tools\src\xgboost\include\xgboost\c_api.h(29): error C2146: syntax error: missing ';' before identifier 'bst_ulong'
Below is a little more information on what I did to get XGBoost working with a console app and how I created the DLL project.
=-=-=-=-=-=
Here's what I did to use XGBoost with a console. Everything about it seems to work. I've tested the model predictions, and they are
consistent with what I'm seeing in R.
Using the documentation found here:
https://xgboost.readthedocs.io/en/latest/build.html
I downloaded the XGBoost source and built it using CMake and Visual Studio 2015.
Under Project > Linker > Input > Additional Dependencies,
I added the xgboost.lib file
Under Project > Linker > General > Additional Library Directories
I added a reference to ...\xgboost\build\Release
Under Project > VC++ Directories > Include Directories
I added the path to ...\xgboost\rabit\include and ...\xgboost\include
I put the xgboost.dll in the directory where the .exe is generated.
From here, it was smooth sailing.
=-=-=-=-=-=
Here's what I've done to create a Visual C++ DLL Project:
After choosing File > New > Project, I select an ATL DLL Project (this is part of the project requirement).
For the XGBoost include to the project, I repeated steps 1-5 above, but placed the xgboost.dll file where the
project DLL would be generated.
Here is the source for the header file for the simple class I have created:
#pragma once
class XGB_Driver
{
public:
XGB_Driver();
~XGB_Driver();
float callXGB(float sample_input);
};
Here is the source for the simple cpp file:
#include <xgboost/c_api.h>
#include "stdafx.h"
#include "XGB_Driver.h"
XGB_Driver::XGB_Driver()
{
}
XGB_Driver::~XGB_Driver()
{
}
float XGB_Driver::callXGB(float simple_input) {
BoosterHandle my_handle;
return(0);
}
Why this doesn't work for the ATL DLL project, but does for the console app really has me banging my head against the keyboard.
Your help would be very much appreciated.
Best,
Dave

With the help of CristiFati, I believe this question has been answered.
For whatever reason, it seems that using XGBoost with C++ in a DLL project requires additional includes above and beyond what is required for using it in a console application.
I am currently building the DLL project with the addition of:
#include <cstdint>

Related

VS2019:fatal error C1083 Cannot open header file: 'opencv.hpp'

It is all fine when I was writing the code, that is, when I included the header file "opencv2/opencv.hpp",and VS2019 can indeed "see" it,in other words, I can use the class that is in the opencv.hpp, but only when I complied the project,c1083 occurred
before i added the function DrawLine(),i compiled for times,with no err.and then i added the function DrawLine(..),error occurred
this is the total source code
a.ha.cpp
//a.h
#include<opencv2\opencv.hpp>
using namespace cv;
class Canvas{
Mat mat_canvas;
void Init(){
mat_canvas.create(1024,1024,CV_32FC3);
}
void DrawLine(float ax,float ay,float bx,float by);
}
//a.cpp
#include"a.h"
void Canvas::DrawLine(float ax,float ay,float bx,float by){
cv::line(this->mat_canvas,Point2f(ax,ax),Point2f(bx,by),Scalar(1,1,1));
}
error msg was:
fatal error C1083 Cannot open header file: 'opencv.hpp': no such file or dir
i know i can create a new solution and move my old solution to it,but it takes time,and i can't actually know whether this issue will happen again in my new solution
Any answer will be helpful
Actually there are most of the time possibility of missing "Additional include directory path" or wrongly setup.
Where to find in Visual Studio:
To set this compiler option in the Visual Studio development environment
Open the project's Property Pages dialog box. For details, see Set
C++ compiler and build properties in Visual Studio.
Select the Configuration Properties > C/C++ > General property page.
Modify the Additional Include Directories property.
Visual Studio 2019 C++ Project configuration
Example project setup is described in below link:
Example setup

LINK : fatal error LNK1181: cannot open input file 'opencv_world341d.lib'

I have found two similar questions this and this .
But they both use opencv, and opencv indeed provide the corresponding lib. I don't use opencv, and my project is very simple, just hello world.I have changed project default configuration like this
except for these configurations, others all take defaults
I just want to test my project configuration,that works find for win32 debug and release. But not work for x64 debug and release, they all tell me LINK : fatal error LNK1104: cannot open file 'opencv_world341d.lib'
I indeed know my project does not use any opencv lib, but why they tell me I need to use opencv_world341d.lib
my code
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
Thanks for everyone who comments on the question. I have solved the problem, although this problem not relevant to OpenCV to much, but I think the solution to the problem may be helpful to others. When I build project, visual studio 2019 tell me cannot link opencv_world341d.lib, so I go to Project->Properties->Linker->Input->Additional Dependencies , and I found opencv_world341d.lib. So I need to remove it, but it's readonly. From this we know visual studio using settings file (Microsoft.cpp..users.props) to control global settings including Global search path. These files are located at $(USERPROFILE)\appdata\local\microsoft\msbuild\v4.0 directory. Then I reedit Microsoft.cpp.<Platform>.users.props, delete opencv_world341d.lib, reboot visual studio, problem solved.

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console

error C2995: 'getVector' : template function has already been defined

I am using VC 6.0 and While compiling the code i am getting this error "error C2995: 'getVector' : template function has already been defined"
I have included the value of WIN32COMMON in the project Settings->C++ tab and in the
category combo box as Preprocessor and in the preprocessordefinition text box as _WIN32COMMON and in the Addition include Libraries text box the complete path as "E:\app\user\product\11.1.0\db_1\OCI\include". in this include folder all the header files are present such as "occi.h".
I also tried with defining WIN32COMMON in my code Explicitly as follows
#include<iostream>
#define WIN32COMMON
#include <occi.h>
using namespace oracle::occi;
using namespace std;
but then it started me giving 102 errors stating that 'DbManager undeclared identifier'.
i had also tried with including the addition libraries in the project settings->link tab->
and in category combobox i selected Input and in the Additional library path i gave the complete path for .lib files as "E:\app\user\product\11.1.0\db_1\OCI\lib\MSVC\vc8" but that also did'nt work. can somebody please guide me.Where iam lacking or i need to define some thing else in my code.
MSVC++ 6.0 doesn't support partial ordering of function templates. Try to replace getVector() with getVectorOfRefs() in your code.
Oracle added getVectorOfRefs() into OCCI to support older Microsoft compilers, such as MSVC6, and recommends to use it in place of getVector().

C++ - Using Hunspell with MFC

After I managed to get Hunspell working with a Win32 console application, I want to use Hunspell in an MFC application in the same solution.
I get the following error when I try to compile the MFC application.
spellcheck\hunspell-src\hunspell\replist.hxx(23): error C2062: type 'const char' unexpected
spellcheck\hunspell-src\hunspell\replist.hxx(23): error C2238: unexpected token(s) preceding ';'
Some information about the solution:
The solution is called spellcheck.
The Hunspell source files are located in spellcheck\hunspell-src\hunspell and spellcheck\hunspell-src\win_api.
The project libhunspell has been imported and compiles without errors.
A console application in the solution uses the same hunspell dll/source files and has no errors.
The console application has been setup accordingly to this thread
The folders HunSpell-Src and HunSpell-Dic are NOT located in a project folder but in the solution folder spellcheck and referenced properly.
I have setup the MFC application as following:
Added a new MFC application (SDI) as project to the solution called MFC2.
Added a reference to the project libhunspell to MFC2.
Project > Properties > Configuration Properties > C++ > General > Additional Include Directories has been set to: ..\hunspell-src\hunspell;%(AdditionalIncludeDirectories) (the same as the console application)
Project > Properties > Configuration Properties > Linker > General > Additional Library Directories has been set to: ..\hunspell-src\win_api\Debug_dll\libhunspell;%(AdditionalLibraryDirectories) (the same as the console application)
Added #include "../HunSpell-Src/win_api/hunspelldll.h"under #include MainFrm.h in Mainfrm.cpp.
I decided to put the #include of hunspelldd.h in Mainfrm.cpp and omit setting up a dialog and calling it in order to keep this question as short as possible. Nevertheless I have setup a project as described that produces said error messages.
Because the console application compiles fine, I assume something MFC specific is causing the problem.
Thank you for your help.
Add "#undef near" before class declaration in replist.hxx
I'd look for an error in the include file just before replist.hxx, perhaps a missing ending semicolon on a class definition.