VC++ 2010 express: How to make a image file output to the Debug directory when compiling? - visual-c++

I'm creating a simple "hello world" vc++/opencv project.
In my code, I want to read and display an image called "opencv.png":
int _tmain(int argc, _TCHAR* argv[])
{
namedWindow( "show_image", WINDOW_AUTOSIZE );
Mat src = imread( "opencv.png" );
imshow( "show_image", src );
char c = waitKey(0);
return 0;
}
Then I put the "opencv.png" under the project root path. But I found when I compiling the project, the "opencv.png" won't be displayed.
In the "helloworld/Debug" directory, there are only 3 files:
helloworld.exe
helloworld.ilk
helloworld.pdb
I have to copy the "opencv.png" manually to "Debug". How to configure the project to let it copy the "opencv.png" to output dir when compiling?
Per James' answer, I think it's a little complicated. Is there any easier way, e.g. embed the "opencv.png" in the final exe file?

You can create a Custom Build Step to perform the copy.
Alternatively, consider placing the image in some known location so that it does not need to be copied (e.g. in a TestData directory), or pass the path to the image via the command line (you can add arguments to the command line in the Debugging page of the project properties).

Related

How to do file inputs via node.js using emscripten?

I have a C++ project that I have converted into javascript using emscripten. I need help with implementing file input into the program via node. As I understand it the default file system in emscripten uses preloaded data that can only be done on a web page or web worker. I need mine to work with node.js on the command line.
Looking at the documentation I see that there's a way to use NODEFS instead of the default MEMFS which should allow me to do this. However, I'm unsure how I'm supposed to go about this. I don't really understand the test code that's provided.
Here's how the file handling is being done in the original C++ project:
void InputFile(std::string &fileName)
{
std::ifstream in(fileName);
if (in.fail())
{
std::cerr << "ERROR, Could not open " << fileName << std::endl;
exit(1);
}
}
But when I attempt to run the converted program with a file, node project.js -f test.file I get the error message: ERROR, Could not open test.file meaning that opening the file failed. The original C++ project was able to open the file without any issues, so I know there's not problem with the file itself.
I'm not sure what I have to do to make the converted project work with file inputs, any help would very much appreciated.
Explanation
WebAssembly module, built using emscripten, has no information about files in your physical file system. Instead, it uses a virtual file system. All you have to do is to create a link between files on your physical system to the files on the module's virtual system. NODEFS gives you this opportunity.
Quick solution
We will start at modifying your C++ code by adding the aforementioned link between physical and virtual file systems using embedded JS code (with EM_ASM). First (1), we create a directory '/temp' on the virtual file system where all referenced files will be located in. Then (2), we link this new virtual directory with a real physical location (the current working directory '.') where all the referenced files are already.
#include <emscripten.h>
#include <emscripten/bind.h>
#include <iostream>
#include <fstream>
void InputFile(const std::string &fileName)
{
EM_ASM(
FS.mkdir('/temp'); // (1)
FS.mount(NODEFS, {root : '.'}, '/temp');); // (2)
std::ifstream in(std::string("/temp/") + fileName);
if (in.fail())
{
std::cerr << "ERROR, Could not open " << fileName << std::endl;
exit(1);
}
}
EMSCRIPTEN_BINDINGS(Module)
{
emscripten::function("InputFile", &InputFile);
}
Now, because in the WebAssembly module, we are working with the virtual file systems, and not the physical one, each referenced file from the current directory (the root '.') is actually in the virtual directory previously linked ('/temp'). Hence, '/temp' directory precedes the name to the referenced file: std::ifstream in(std::string("/temp/") + fileName);.
Finally, we can compile this file. We force the synchronized compilation (to make sure the require loads the WASM module on time). Moreover, the option -s EXIT_RUNTIME=1 makes sure that the C++ command exit(1); finishes the execution. Also, we need to link Embind (--bind) and NODEFS (-lnodefs.js):
emcc project.cpp -o project.js -s WASM_ASYNC_COMPILATION=0 -s EXIT_RUNTIME=1 --bind -lnodefs.js
Testing
To test the WebAssembly module with the same calling convention as you have mentioned, we can use the following test.js script:
var Module = require('./project.js');
if (process.argv[3] && process.argv[2] === '-f') {
const filename = process.argv[3];
Module.InputFile(filename);
} else {
console.log('Pass the file with -f flag!');
}
To run the file, all you have to do is this: node test.js -f test.file
Comment
This approach works well if the referenced files are in the current working directory. In the case they are not, you could modify the code of the InputFile to extract the directory in which the fileName is, and then, mount the real-to-virtual directory accordingly.

How to get NReco.PdfGenerator.LT working with Linux binaries?

I'm using NReco.PdfGenerator.LT to create a PDF document from a HTML page. This works fine on windows if I just point it at the wkhtmltopdf.exe executable, but this does not have the same effect on Linux.
HtmlToPdfConverter cvt = new HtmlToPdfConverter();
cvt.PdfToolPath = GetPdfToolPath();
// Windows: <current-dir>\wkhtmltopdf\win32\bin
// Linux: <current-dir>/wkhtmltopdf/linux/bin
cvt.WkHtmlToPdfExeName = GetPdfToolName();
// Windows: wkhtmltopdf.exe
// Linux: wkhtmltopdf
I just get this error:
Cannot generate PDF: Permission denied
Possible reasons of this error:
linux user who starts .net core program has no permissions to run "wkhtmltopdf". Also ensure that "wkhtmltopdf" file is marked as "executable" (x).
sometimes temp folder returned by Path.GetTempPath() is not accessible; another location for temp files may be specified with HtmlToPdfConverter.TempFilesPath property

Rcpp & RInside link error

I am running R3.1 on Kubuntu 14.04 and using Codeblocks as an IDE for trying an RInside program from Dirk Eddelbuettels eg:
http://dirk.eddelbuettel.com/blog/2011/03/25/#rinside_and_qt
`
// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
//
// GPL'ed
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
R["txt"] = "Hello, world!\n"; // assign a char* (string) to 'txt'
R.parseEvalQ("cat(txt)"); // eval the init string, ignoring any returns
exit(0);
}
`
and i get the following error:
/usr/bin/ld: obj/Debug/main.o||undefined reference to symbol 'REprintf'|
and I don't know what library is missing. Anyone know?
Thanks
That is the environment I develop on (apart from the difference that I am now on 15.05) which is almost certain to work -- but you got an error indicating that you did not link against libR.
That is almost surely due to you not doing what the README suggests ie for the dozen+ examples in directory examples/standard/ do
make # compile and link all
./rinside_sample0 # run the first
If you insist on using an IDE you can use the contributed cmake/ directory. But the code is tested and released with for use with the (GNU)makefile.

Unable to load so file from Java in Eclipse On Ubuntu

I have some code that tries to load a C library as follows :-
public ThreadAffinity() {
ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
}
However I get the following error when trying to build the project; The error I get is as follows :-
UnsatisfiedLinkError: Unable to load library 'libctest': liblibctest.so: cannot open shared object file: No such file or directory
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:166)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:239)
at com.sun.jna.Library$Handler.<init>(Library.java:140)
at com.sun.jna.Native.loadLibrary(Native.java:393)
at com.sun.jna.Native.loadLibrary(Native.java:378)
at com.threads.ThreadAffinity.<init>(ThreadAffinity.java:11)
at com.threads.ThreadAffinity.main(ThreadAffinity.java:45)
The current working directory is the root of the project and thats where the so file is located. I also tried modifying the LD_PRELOAD variable to point to my so file; however the error persists.
It works just fine on my OSX where the dylib is located exactly where the so file is currently(project root).
What am I doing wrong?
From the exception:
UnsatisfiedLinkError: Unable to load library 'libctest': liblibctest.so: cannot open shared object file: No such file or directory
It implies you used something like:
public ThreadAffinity() {
ctest = (CTest) Native.loadLibrary("libctest", CTest.class);
}
and not:
public ThreadAffinity() {
ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
}
hence you see the JNA added prefix of lib and postfix of .so added to libctest (liblibctest.so)
LD_PRELOAD is used when you want to prefer one particular version of the same shared library over another, which doesn't apply here.
Define jna.library.path to point to your project root, and JNA should be able to find it.
Also make sure your library has been built as libctest.so and wasn't inadvertently named libctest.dylib.

How to deliver a node.js + imagemagick tool?

I've developed a node.js tool which uses imagemagick from the command line. That is, exec('my_imagemagick_commands'). What's the best way to deliver that tool to a client using Windows? That is, how can I make a Windows installer that'll install node.js, imagemagick and the tool - preferably as a binary, not the source - in a specific folder?
If you want an easy bundle... Zip the list below, deploy on client and drag/drop images to be processed onto the yourtool.cmd file (I'm doing something similar for image optimizers)
Bundle: (put these in one directory)
yourtool.cmd
yourtool.js
node.exe
node_modules/ (if applicable)
yourtool.cmd
REM Get the drive/path the batch file is in
set batchdir=%~d0%~p0
REM Run tool for items dragged over...
"%batchdir%node.exe" "%batchdir%yourtool.js" %*
yourtool.js
// start at 2 for arguments passed...
// 0 is node.exe
// 1 is the js file run
for (var i=2; i<process.argv.length; i++) {
var imagePath = process.argv[i];
//do something with image...
}
For others interested in imagemagick with node, you should check out node-imagemagick

Resources