Setting up NLOpt when working from windows cmd prompt - c++

I am writing a c++ program from the windows cmd prompt, and compiling with mingw (g++). This program needs a non-linear optimiser and NLOpt looks like a good choice. I learnt c++ for a uni course, so the environment I was using was already set up, I have no experience in setting up libraries etc.
So far my steps have been;
Download the precompiled DLLs for 64-bit windows (which is what I'm running) from here.
Run the command dlltool --input-def libnlopt-0.def --dllname libnlopt-0.dll --output-lib libnlopt-0.lib (from the same page), which ran without errors.
This creates an .hpp file, however, when I try to #include the file I get
In file included from optimiseDogs.cc:9:0:
C:\Files|Development\NLOpt2.4.2\nlopt.hpp:29:19: fatal error: nlopt.h: Nosuch file or directory
#include <nlopt.h>
nlopt.h and nlopt.hpp are in the same directory as each other. My program is in a different folder.
This is probably really basic, sorry to trouble you with it. Perhaps I am not completely alone in my ignorance and this will help someone else too. Also, I have seen this question, but it deals with installing on visual basic, and I'm not using a GUI, just notepad++ and the cmd prompt.
Thank you for your help.

If you are in windows and using mingw: downloaded zip archive already contains .dll and .lib files. So you dont need to run dlltool which creates these .lib,.dll from .def. I think your problem is compiling command using g++ adding include and library path directives.
C:\Files\Development\NLOpt2.4.2>g++ <path_of_source>.cc -o program.exe -L. -lnlopt -lm -I. -I<path_of_your_headers>
I encounted in the past that order of -I and -L may matter so try changing if not working.
Please see this tut and that tut to understand the concept.

Related

Problems Including GTK for C++ on Manjaro Linux

I've been struggling for hours to include the GTK library for manjaro in C++. I'm using VS Code, I have installed the GTK-4.0 package with the command sudo pacman -Syu gtk4 and I'm including the file /usr/include/gtk-4.0/gtk/gtk.h in my C++ program with the line #include <gtk-4.0/gtk/gtk.h>. It's throwing the error
gtk/css/gtkcss.h: No such file or directory
even though there is a file named gtkcss.h at the path /usr/include/gtk-4.0/gtk/css/gtkcss.h
I've looked all over google and I can't find anyone having the same problem, especially on the same OS. Any help is much appreciated!
You are missing an include path.
Gtk requires* pkg-config to set the include path. see https://developer.gnome.org/gtk4/unstable/gtk-compiling.html
So while you have included it with a complete path, the files inside the library still require the include path to be set.
I don't know which build system you are using, but in general gtk requires something like:
g++ $(pkg-config --cflags) -c main.cpp
This will expand to the right flags to set the include path.
*You might also do this manually by adding a -I flag. But that is prone to break if you want to compile on a different machine.

HDF5 + Eclipse CDT errors

I'm attempting to edit a big 'ol wind tunnel simulation to spit output files in the .hdf5 format rather than the current format, .CSV.
The entire simulation currently runs on Eclipse CDT, and is compiled & linked using MinGW g++ on a mix of Windows and Linux platforms; I am using a Windows 7 Enterprise device. My job is to edit a few lines of the simulation's source code to output files in the HDF5 format.
The problem is -- I can't get anything to link without a hodge-podge of "undefined reference" errors. I started out by using the HDF5 1.8.19 distributions for windows, but the included .lib + .dll files are specifically designed for the MSVS2013 compiler. I have since switched to using the Linux line endings due to g++ compatibility, but I still get a multitude of "undefined reference" errors.
I'm linking the libraries and include files, and I'm also linking their search paths. The issue seems to be that for some reason the MinGW linker can't read the symbol names in the libhdf5_cpp.a files.
Any thoughts? Does anyone have experience trying to interface eclipse with HDF5?
I'm just trying to run this example code.
My output errors are as follows:
g++ "-LC:\\Users\\jk5759\\workspace\\HDF5_Test" -o HDF5_Test.exe main.o -lhdf5_cpp -static
main.o: In function `main':
C:\Users\jk5759\workspace\HDF5_Test\Debug/../main.cpp:14: undefined reference to `H5check_version'
C:\Users\jk5759\workspace\HDF5_Test\Debug/../main.cpp:14: undefined reference to `H5Fcreate'
C:\Users\jk5759\workspace\HDF5_Test\Debug/../main.cpp:17: undefined reference to `H5Fclose'
collect2: ld returned 1 exit status
In general, C++ libraries have to be explicitly build for a specific target compiler due to name-mangling issues, differing binary specifications for exceptions, etc. Your best bet is probably going to be to build the HDF5 library from source using MinGW and use that.
See also here (MinGW docs).
I'm going to answer this question myself, because there simply isn't any resource on the web that really talks about using HDF5 + Windows + MinGW GCC + Eclipse.
Instructions for installing HDF5 for use with the following:
-Eclipse CDT Neon
-Windows 7 Enterprise
-C++ Programming
-MinGW GCC Compiler
1) Make certain you're logged in with admin privelages. MinGW and MSYS, as well as HDF5, may not install correctly without admin rights.
2) Obtain HDF5 version 1.8.2. This is available via the following URL: https://support.hdfgroup.org/ftp/HDF5/prev-releases/hdf5-1.8/hdf5-1.8.2/src/
Make sure to get the .tar version. This folder contains the source code only, which will still need to be edited in the future.
Unpack the tarball and move it to a directory where you can easily find it. Mine is labelled "MyHDFstuff", and is in the "Documents" folder.
Once the tarball is unpacked, find all files named H5pubconf.h and append the following lines to the end of the program:
There should be two H5pubconf.h files, one in the /src/ folder and one in the /windows/src/ folder.
/* ~~~~ User Added Code ~~~~ */
#ifndef H5_HAVE_WIN32_API
#ifdef WIN32 /* defined for all windows systems */
#define H5_HAVE_WIN32_API 1
#endif
#endif
#ifndef H5_HAVE_MINGW
#ifdef __MINGW32__ /*defined for all MinGW compilers */
#define H5_HAVE_MINGW 1
#endif
#endif
//fixes second problem
#define H5_BUILT_AS_DYNAMIC_LIB 1
3) Obtain MSYS linux command-line emulator for windows. This is a tool designed to interface with MinGW and provide Lunix-like utilities.
The ones of particular intrest to us are the "./configure", "make", and "make install" commands. It can be obtained here: http://www.mingw.org/wiki/MSYS
4) Launch MSYS. Navigate to the "MyHDFstuff" folder within MSYS using the "cd" command. Run the following commands from the MSYS prompt:
$ ./configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --disable-hl --prefix=$HOME
$ make
$ make check
$ make install
This will build the HDF5 libraries for the MinGW compiler. You should get an error -- ignore it, it's not going to stop your code from compiling and running.
5) In your eclipse project settings, make the following changes:
-add "hdf5-1.8.2\c++\src" to your include paths. this is most of the c++ utilities
-add "hdf5-1.8.2\src" to your include paths. This is most of the HDF5 C root utilities
-add "hdf5-1.8.2\src.libs" to your library folder search paths (-L)
-add "hdf5" to your libraries (-l)
** Note, this will not get you all the way to using the C++ API interface, but you will be able to program using the C commands (https://support.hdfgroup.org/HDF5/doc1.8/).
If these steps are not followed, then you will be able to use several of the C-interface commands, including file creation, but your executable will fail because it will be
unable to find the datatypes interface.
*** I will attach an example C++ program and output file in the future for reference
Happy coding!

How to build Crypto++ library on Linux with MinGW?

im trying to port a c++ project from visual studio 2013 to netbeans on ubuntu. The target is a windows executable, so far im able to compile windows exe files from netbeans using mingw as compiler.
The project needs the Crypto++ library and i only have the .lib version for visual studio. To include the library in netbeans i first need to build the Crypto++ library in the .a format.
Its not clear to me how should i do that.
Ive found informations on how to build the library for linux with mingw but not for cross compilation with mingw. The provided GNUmakefile does not work.
Should i set up a vm with windows and mingw and compile the library that way? Or maybe use the qmake "hack" as suggested here http://www.qtcentre.org/threads/28809-Compiling-amp-using-Crypto-with-mingw-version-of-Qt ? The last good Crypto++ version suggested there is quite old. This is confusing, porting the whole project is easier than having the required library.
Im open to any suggestion.
tldr: how to build libcryptopp.a on linux for the cross compilation of a windows exe project
edit:
for example if i cross compile something there are windows libraries in the /usr/i686-w64-mingw32/lib/ folder like libuser32.a. i need to make the equivalend libcryptopp.a.. sorry if is a bit unclear.
if i simply build using the steps in answer (and in the wiki) i end up with a libcryptopp.a file but i suspect is linux-only, cause if i link that library in netbeans in the .exe file im cross compiling i end up with undefined references to cryptopp stuff everywere. the paths are correct, i suspect the library needs to be replaced with the equivalent libcryptopp.a compatible with crosscompiling.
edit2: im trying to follow the answer down here, now im stuck here. after this command to build cryptopp.
make CXX=/usr/bin/i686-w64-mingw32-gcc INCLUDES="-I /usr/i686-w64-mingw32/include" LIBS="-L /usr/i686-w64-mingw32/lib" CXXFLAGS="-std=c++0x"
and many variations of it i always end up with this error
trap.h:26:25: fatal error: Windows.h: No such file or directory
# include
which makes me think its using the right compiler to make a .a lib file for cross compiling the windows .exe with the lib.
however i dont understand whats going on now with the missing header..:(
Also if i try to link the lib file (used with visual studio) i get a lot of linking errors, undefined reference to cryptopp stuff.
ill offer a symbolic beer (a couple of $ of Bitcoins) if someone finds out how to do it.
Inspired by http://wiki.amule.org/wiki/Cross-compilation_for_windows_with_mingw:
sudo apt-get install mingw-w64
git clone https://github.com/weidai11/cryptopp
cd cryptopp
export TARGET=i686-w64-mingw32
CXX=$TARGET-g++ RANLIB=$TARGET-ranlib AR=$TARGET-ar LDLIBS=-lws2_32 make -f GNUmakefile
I've found informations on how to build the library for linux with mingw but not for cross compilation with mingw.
Its relatively easy...
Get Crypto++ ZIP into MinGW.
Unpack Crypto++ ZIP.
Change directories.
Build the library.
Verify All tests passed.
(1) and (2) can be tricky because Cygwin and MinGW are missing a lot of tools. So curl https://www.cryptopp.com/cryptopp563.zip -o cryptopp563.zip may not work.
For (2), I seem to recall ZIP is missing, so unzip -aoq cryptopp563.zip -d cryptopp-5.6.3 may not work.
At step (4), just perform make static dynamic test and be sure it finished with All tests passed.
I do a lot of testing with Cygwin and MinGW. I have a script that copies Crypto++ into the environments from my Desktop. I have not been able to figure out a way to automate it. A recent question on automating it was closed, so no one can supply an answer (see How to automate software testing for Cygwin and MinGW).

How to install protobuf on windows? (Win7x64/MinGW)

C++-Protobuf does not compile in VS2012. Now I want to use MinGW to compile it on windows. Can someone please give me some brief headwords on how to compile protobuf on Win7 x64. I already installed MinGW with the GUI installer. Google writes as MinGW setup notice that I should refer to the Unix installation notes. But I cant figure out how to use the auto tools on windows.
Edit
Okay this is what I've done until now:
$ mount C:/ WinDir
$ cd ./[...]/protobuf.2.4.1
$ ./configure
$ minGW32-make.exe
$ minGW32-make.exe check
minGW32-make.exe runs without errors, but no tests are running and I cant find libprotobuf.lib. There are some libprotobuf.dll but I need the lib, dont I?.
You should have an MSys console together with your MinGW instalation. This console provides an linux-like environment in which you should be able to use autotools normally.
If MSys is not installed, you can grab it from the MinGW site too.
cd to your directory with sources and try the usual:
$ ./configure
$ make
Some libraries cause problems on Windows but most compile well with MinGW and MSys. Come back and add more info to your question if you run into specific problems.
Edit:
minGW32-make.exe runs without errors, but no tests are running and I cant find libprotobuf.lib. There are some libprotobuf.dll but I need the lib, dont I?.
Usually for a dynamic library you'd get protobuf.dll (the dynamic library) and libprotobuf.a (the static wrapper library).
When linking, just pass -lprotobuf to the linker - it will look for both libprotobuf.a and protobuf.lib.
(.lib is another static library format, which is partially handled by MinGW but not native here.)
You will not work with a .lib file when using the MinGW toolchain. Instead, you are able to link against the dll directly. The MinGW Wiki explains this.
I could get dll and lib both. This is when you do not want static lib file and want to use dll and lib file.
You need to make following changes in Protobuf code:
Open the project in VS. Or any other editor. I use VS2015.
In libProtoBuf project settings, in C/C++ Preprocessor add following flags.
PROTOBUF_USE_DLLS; LIBPROTOBUF_EXPORTS;
Those flags will export information from profobuf using dllexport
in ur client code where you are using Protobuf, define: PROTOBUF_USE_DLLS. Which will make protobuf includes to use dllimport.
Once you do step 2, you will see both dll and lib in your output folder. Otherwise, you will always see just dll and not lib file.
Hope this helps. If not, please write a message here and I can help you getting this sorted out.

Code Blocks and SDL output. weird behavior

I dont know if my codeblocks has something wrong but I can compile the project but I cannot run it from the program itself it flashes and then the SDL aplication closes and the console appear(IDK but this console doesn't seem like the tipic console).
I can go and run the program by myself but seriously is not the way it should be right?
And also I have noticed that when you run a console aplication not SDL aplication the console is slighty different? why, is there a way to solve this? has someting to do with the console?
I assume your using code blocks in windows.
The reason for the difference in the console is because code blocks uses Msys which is a minimal implantation of a POSIX (*nix) shell environment.
Msys treats paths in windows like *nix paths C:\folder\file.ext can be access via /c/folder/file.
MSYS works together with MinGW to provide a *nix like environment and build chain, including GCC, make, auto-conf and a few others, you can also build most open source libraries fairly easily in the same way you would on a *nix system: "./configure" "make" "make install".
There are a few things you should check that your doing right when compiling SDL with MinGW.
g++ seems to work better than gcc in my experience.
You need to set up the options going to g++:
-g -O2
include Dir:
-I/mingw/include/SDL
Defines, you can also replace the -D with #define and put at the top of your source:
-D_GNU_SOURCE=1 -Dmain=SDL_main -DHAVE_OPENGL
Lib folder
-L/mingw/lib
Libraries:
-lmingw32 -lSDLmain -lSDL -mwindows
These setting might not work for you but there pretty close, so check where the SDL.h and lib file is and adjust if necessary. Though those are probably set up right if it is compiling.
Its fixed i just had to copy the images to same folder as the project silly of me =P