build R package with C code (without Rcpp) - c++

environment:
OS:Windows10 [10.0.14393]
R:3.4.1 (2017-06-30) -- "Single Candle" 32-bit
Rstudio:1.0.143
Rtools:3.4.0.1964
This is my first time to build R package (named testt).
I'm trying to build a package with some C code without using Rcpp.
I have put all .c files (called lca.c, rlca_cond.c, rlca_prev.cand rlca_condprev.c)
and their header file in /src.
And I have some C function defined in chanmat.c, declared in chanmat.h.
chanmat.c and chanmat.h are also in /src.
Both lca.h, rlca_cond.h, rlca_prev.h and rlca_condprev.h have #include 'chanmat.h'.
In chanmat.h, I use the following to prevent multiple definition when compiling.
#ifndef CHANMATH_H
#define CHANMATH_H
...
#endif
I have also used following in NAMESPACE file (generated by roxygen2)
useDynLib(testt,lca)
useDynLib(testt,rlca_cond)
useDynLib(testt,rlca_prev)
useDynLib(testt,rlca_condprev)
useDynLib(testt,chanmat)
When I run build & reload, I got following error
Updating testt documentation
Loading testt
Error in FUN(X[[i]], ...) :
no such symbol chanmat in package
C:/Users/elephant/Desktop/project/testt/src/testt.dll
Calls: suppressPackageStartupMessages ... assignNativeRoutines ->
getNativeSymbolInfo -> lapply -> FUN
��������
Exited with status 1.
I have no idea why chanmat.c/chanmat.h are failed to compile.
This question bothers me for a long time.
Any thoughts on how to fix this?

Related

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

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>

using lp_solve with visual studio 2015

I'm rather new to C++. I'm trying to use VS2015 and the lp_solve library(https://sourceforge.net/projects/lpsolve/files/lpsolve/5.5.2.5/) to solve my linear eq problem(for exact Gromov-Hausdorrf Distance, see https://mediatum.ub.tum.de/doc/1231885/1231885.pdf section 3.1 and 3.3). My machine is x64 system. Here is what I've done:
donwload the lp_solve_5.5.2.5_dev_win64.zip package and decompress it.
specific the library directoy(through project -> properties -> linker)
specific lib I used (liblpsolve55.lib and liblpsolve55d.lib for statically linking) through project->properties->linker->input->additional dependency.
declare the .h file directory(through project->properties->C/C++).
Here is what I declare in my code:
#include "lp_lib.h"
#pragma comment(lib, "liblpsolve55.lib") // static
#pragma comment(lib, "liblpsolve55d.lib") // static for debug
While when I run the code with x86, it will show the following problems:
error LNK2019: unresolved external symbol _make_lp#8 referenced in function "void __cdecl my_solve [totally 13 errors like this]
warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64' [totally 2 warnings like this]
While when I switch the runner with x64:
I got 550 errors, mainly due to the “dlfcn.h”: No such file or directory
in the lp_lib.h file, it has lines:
#if (LoadInverseLib == TRUE) || (LoadLanguageLib == TRUE)
#ifdef WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#endif
which means it has treat me as a linux machine since dlfcn.h is for unix/linux
I've also try to use the 32bit version package(lp_solve_5.5.2.5_dev_win32.zip), but it do not work either.
with x86 runner:__iob, _printf issues.
with x64 runner:“dlfcn.h”: No such file or directory
I've successively build and run the demo in my mac with the corresponding package(lp_solve_5.5.2.5_dev_osx32.tar.gz). But I still want to know how to build and run it in VS2015.
Here I'll state what I do in mac if anyone need it(using Xcode).
download the lp_solve_5.5.2.5_dev_osx32.tar.gz package
add all the .h file in build phase -> headers
add the liblpsolve55.a file in build phase -> link binary with libraries
(if you want to use .dylib as dynamic linking, add it to the build phase -> copy file, but it do not work for me)
in build setting, set the Architectures to 32-bit intel
in build setting, set the Build Active Architecture Only to No
Thank you very much, actually nobody answer this question :) But I've solve the problem myself. Here is what I do for VS2015:
use package lp_solve_5.5.2.5_dev_win32.zip
add the .h file and .lib as above
change the run library, through properties -> C/C++ -> code generation -> run library -> change the DLL(/MDd) to (/MTd)
if you still got some fprintf issues, go to properties -> linker -> input -> additional dependency, and add : legacy_stdio_definitions.lib
That will be OK. That my be some translation issue, yes I use the chinese-version VS...

C++ - no rule to create target SaltPepper.o

I know there are several questions like this, no answer I have found so far seems to solve my problem.
I am using eclipse.
I coded something in the main function, and it worked fine.
I then an external function with the code in the main and now I get funny mistakes.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <random>
#include <ctime>
#include <cstdlib>
using namespace std;
using namespace cv;
void createSaltandPepper();
int main(int argc, char** argv) {
createSaltandPepper();
return 0;
}
No, as I am trying not to give to extensive an example again, here is the outside of the function:
void createSaltandPepper() {
//mycode
}
At least it does not seem to be a spelling mistake.
However, the error is:
make all
make: *** No rule to make target 'SaltPepper.o', needed by 'Display'.
I do wonder why it is trying to make a tagert called SaltPepper.o if my function is called createSaltandPepper.
Can somebody help me?
Edit: I did not conciously create a makefile, as I said I am working with eclipse.
And again, the code works fine as long as it is inside the main function instead of inside createSaltandPepper().
My file is called DisplayImage.cpp, the code above is in this file, including the function createSaltandPepper();
The only two function in this file are the main function and createSaltandPepper();
There are no other source files in the project.
I am sorry if I come across as rather stupid: I am a Java programmer and have not a clue about makefiles and such.
See this screenshot:
I now created a new project and added my old file into it, this time calling it Display.cpp
"build all" resulted in an error in subdir.mk which reads:
subdir.mk:18: recipe for target 'Display.o' failed
make: *** [Display.o] Error 1
The complete Error message for the build is as follows:
make all
Building file: ../Display.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0-std=c++11 -MMD -MP -MF"Display.d" -MT"Display.d" -o"Display.o" "../Display.cpp"
subdir.mk:18: recipe for target 'Display.o' failed
g++: error: argument to ‘-fmessage-length=’ should be a non-negative integer
make: *** [Display.o] Error 1
Update
I switched to Netbeans.
It now works.
First, this error has nothing to do with compilers, this error message is produced by the build system (make in your case). Basically, eclipse seems to call make, which figures out what are dependencies between files and which of them need recompiling and in turn calls the compiler to compile them. Every C++ source file is then compiled to an object file .o (typically with the same name: SaltPepper.cpp -> SaltPepper.o). Then all the object files are linked together with the libraries to form the final executable.
This particular error message tells you that your executable is specified to depend on the object file SaltPepper.o, but the build system does not know how to produce it. Most likely there is no corresponding SaltPepper.cpp file. So check if such file exists. If not, check the settings of your project (or the Makefile if you created it manually) and see if all the cpp filenames are specified correctly. You should add all the cpp files with your code to the project, and remove all the extraneous cpp files.
Update: Note that build system operates on the file level. It does not know anything about functions etc., only source files. Maybe this is the source of the confusion.

GMock doesn't compile - GTEST_EXCLUSIVE_LOCK_REQUIRED seems to not be defined

I'm trying to build a simple mocked class
#include "interpolation.hpp"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class MockInterp1D : public Interp1DBase {
public:
MOCK_METHOD1(evaluateAt, double(double));
MOCK_METHOD2(evaluateAt, double(double, int));
};
based on the following base class
class Interp1DBase {
public:
virtual double evaluateAt(double) const = 0;
virtual double evaluateAt(double, int) const = 0;
virtual ~Interp1DBase() { };
};
using Google Mocks. When I try to compile the tests where this mock is used, I get the following error:
In file included from /usr/include/gmock/gmock-generated-function-mockers.h:43:0,
from /usr/include/gmock/gmock.h:61,
from /home/tlycken/exjobb/Code/alpha-orbit-follower/test/interpolation/interpolation-mocks.hpp:4,
from /home/tlycken/exjobb/Code/alpha-orbit-follower/test/physics/B-field-tests.hpp:6,
from /home/tlycken/exjobb/Code/alpha-orbit-follower/test/physics/B-field-tests.cpp:2:
/usr/include/gmock/gmock-spec-builders.h:134:41: error: expected ‘;’ at end of member declaration
bool VerifyAndClearExpectationsLocked()
^
and then literally hundreds of similar syntax or definition errors all pointing to files within GMock.
I took a look at gmock-spec-builder.h:134, and found the following (in some context):
// Verifies that all expectations on this mock function have been
// satisfied. Reports one or more Google Test non-fatal failures
// and returns false if not.
bool VerifyAndClearExpectationsLocked()
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
which led me to believe that GTEST_EXCLUSIVE_LOCK_REQUIRED_ might be a macro that for some reason wasn't defined. And indeed, after digging through all header files included from either gmock/gmock.h or gtest/gtest.h I still haven't found the definition of that macro.
What am I doing wrong here?
UPDATE:
I've been able to produce an even simple minimal example:
// in file mock-test.cpp
#include <gmock/gmock.h>
// Yeah, that's the only content
Compile with
g++ -o mock-test.o -c mock-test.cpp
Causes the same error.
I've installed GMock through sudo apt-get install google-mock, which gave me a folder under /usr/src where I could run cmake . followed by make to generate library files which I copied to /usr/lib. The header files were already in /usr/include so I didn't do anything about them manually.
I got the same error when I tried to compile using gmock 1.7 with gtest 1.6. Make sure you are using the same version of gtest.
I don't know how to use g-mock in gcc.
But basically we use in windows like this.
Your class definitions are right.
Did you use those functions like
EXPECT_CALL(classobj, exact functionname as it looks in definition).AtLeast(Times(0)).Return(0);
then
classobj.exact functionname as it looks in definition()
Try this I think it should work. If you want it in detail, just let me know I will expand the same with some typical example.
There are the appropriate sources & headers in the /usr/src/gmock folder.
Your job is only overwrite the whole content of folders:
/usr/src/gmock/gtest/src -> /usr/src/gtest/src
/usr/src/gmock/gtest/cmake -> /usr/src/gtest/cmake
/usr/src/gmock/gtest/CMakeLists.txt-> /usr/src/gtest/CMakeLists.txt
/usr/src/gmock/gtest/include/gtest -> /usr/include/gtest/

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