How to use C++ compiler by Matlab Mex compilation tool - c++

I am trying to build a source code bundle containing m files and c++ (cpp) source files in Matlab.
The source folder has a simple Matlab Script to compile all cpp files in one folder:
function compileDir_simple(Cdir)
if nargin<1
Cdir=pwd;
end
files = dir(fullfile(Cdir,'*.cpp'));
oldDir=pwd;
cd(Cdir);
for j=1:length(files)
try
cm = sprintf('mex -largeArrayDims %s',files(j).name);
disp(cm);
eval(cm);
catch
disp(lasterr);
disp('IGNORE if the file is a C++ file which is not a mex file (ie without a mexFunction inside)');
end
end
cd(oldDir);
Inside, it uses "mex -largeArrayDims". However, my problem is, when I evaluate that statement Matlab tries to build the selected files by a C compiler which is contained in MATLAB itself. When I call mex -setup I see:
mex -setup
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:\MATLAB\R2010a\sys\lcc
[0] None
Compiler:
I installed Bloodshed C++ compiler and added its bin folder ( to the Windows Environment variables, but still I cannot see my C++ compiler in the list of installed compilers. Only Lcc-win32 appears. It will be appropriate to state that it is my first experience on compiling mex files.

A mex file on Windows is just a DLL that exports a function named mexFunction. In principle you can compile mex files with any compiler that can create Windows DLLs.
However, to do so using the mex function in MATLAB requires that MATLAB knows about your compiler. And by default MATLAB only has knowledge of a limited number of compilers. Your chosen compiler is not one of them.
Bloodshed is based on mingw. Which means that you should be able to use the Gnumex project to create a mexopts.bat file for use with your compiler.
However, I would be a little sceptical of using Bloodshed here. It is a C++ IDE and I'm not sure you particular need that. I suspect that all you are looking for is a compiler. In which case you would likely be best served by installing plain mingw.

Bloodshed C++ is not a supported compiler. Check http://www.mathworks.co.uk/support/compilers/R2014a/index.html for a list of supported compilers. Older releases are available from the same page.

Related

Compiling cpp using LLVM 11.0.0 on Windows 10 mashine

LLVM (Clang) newbie question. I have installed the LLVM 11.0.0 on a clear Windows 10 mashine. What do I have to do to get an a.out for -target armv7a-none-eabi?
#include <stdio.h>
#include <iostream>
#include <vector>
int main(void) {
int counter = 0;
counter++;
printf("counter: %d\n", counter);
printf("c++14 output:");
std::vector<int> vect{1, 2, 3, 4, 5};
for (auto & el : vect)
std::cout << "-" << el << std::endl;
return counter;
}
Please write in detail what do I have to do, where to get needed headers, what to put in PATH, etc...
Important:
I need to cross-compile and get an output for -target armv7a-none-eabi
no Visual Studio on that mashine installed
Typically, when installing LLVM for Windows, the path variable is adjusted automatically, so you don't have to modify it. Of course, when installing LLVM, you have to make sure to install all files that are relevant for your build target (in your case: armv7a-none-eabi).
What you have to do is the following:
Run a shell (for example PowerShell) in a terminal.
Change to the folder that contains your source file.
Type clang -target armv7a-none-eabi myfile.cpp (provided you file's name is myfile.cpp) and press enter.
After hat, you have a a.exe file.
The bad news is that you can't really do exactly what you're asking for very easily. Unless you are terribly ambitious or tasked with creating the developer toolchain that can do what you ask, you don't need to bother with the rest below. Switch to a platform like Ubuntu linux that has easily-installed (gcc) cross toolchains, or ask your BSP vendor for a Windows cross toolchain based on clang.
The release tarballs provided by LLVM community at https://releases.llvm.org/ are best suited for native builds. Yes, they include a compiler, assembler, linker, C++ library. They all work together well as a native toolchain (but will refer to the native host C library, and the native host linker by default). Yes, it's true that the compiler/assembler that are included do support all of the LLVM targets. But you don't want just a compiler, you want a toolchain. A complete cross-target C/C++ toolchain will typically include:
OS headers that declare types and functions
C library headers that refer to the OS headers and declare types and functions, library/shared object archive that define functions.
C++ library headers that declare templates, types and functions, library/shared object archive that define functions.
target linker
binutils: standalone assembler, objcopy, objdump, archiver, etc.
With #2 above you could make a call to printf() and the linker would be able to find its implementation in the C library archive. Your linked ./a.out executable could then run that printf code and expect it to emit something to some console or semihosted output.
I am trying to cross-compile from windows to ARM. When I compile with
clang, I get an error missing stdio.h. When I compile with clang++ I
got warning: unable to find a Visual Studio installation. What to do?
When I compile with -target armv7a-none-eabi, I got missing stdio.h.
armv7a-none-eabi describes a target that has no operating system. If we had a stdio.h that declared printf, that would get us part of the way. But what should happen in the printf() implementation for this particular target?
What most users want is a vendor to provide the C library headers and archive -- a commercial or open source distributor who packages up a C library. Usually they're bundled with the toolchain itself. Sometimes the development board and corresponding toolchain come in one big bundle/BSP.
Since you asked for armv7a-none-eabi specifically, I would strongly recommend that you find a vendor to give you what you want. Especially if you need to use a Windows host.
If you aren't stuck on Windows, or are willing to use WSL: Debian and Ubuntu provide cross toolchains and C libraries (including ones like libnewlib-arm-none-eabi). Unfortunately, I don't think there's any clang-based cross toolchain that would leverage this C library.
While you could try to bind libnewlib-arm-none-eabi with a clang tarball from https://releases.llvm.org/, it won't be particularly easy. I would start off by reviewing https://releases.llvm.org/11.0.0/tools/clang/docs/UsersManual.html#configuration-files - create a config file that references the relevant include path(s) and library path(s).
Once you have the config file prototyped, start small and build:
try to compile an object file for your target from int foo(void) { return 33; }.
try to build an executable from a .c file with int main(void) {}
try to build an executable from a .c file with a call to printf().
If you get through those three steps, congratulations, you probably had to figure out a lot of interesting challenges. If you want std::cout and friends to work, you will need to build a C++ library for your target. You can use libc++/libc++abi or libstdc++. C++ libraries on baremetal/freestanding targets are probably not very common. That said, there's lots of C++ library content that has little or no system dependencies. Especially the C++98/03 content that focused on STL - they probably only depend on the system heap allocator. Your example only shows std::vector<> and std::cout, so it's probably doable.

In C++, what is wx.h?

The existing code is calling some sort of wx header file and my DEV C++ compiler just says there's no such file.
Code:
#include<wx/wx.h>
Compiler error:
[Error] wx/wx.h: No such file or directory
So my question is -
What is wx.h
How do I install it in my compiler so that I can use it?
Do I need to change my compiler or DEV C++ would do fine?
What is wx.h
It is the header file of a library. The GitHub project should have some instructions on how to fetch the dependencies and how to install them.
How do I install it in my compiler so that I can use it?
Normally you have to download the dependency, build it (following some instructions), and then you need to use the header in your project and link the library. The exact steps depend on each library and each compiler.
Do I need to change my compiler or DEV C++ would do fine?
In principle, no. Some libraries only work with some compilers, though.
Note that Dev-C++ is not a compiler, it is an IDE that comes with a port of GCC (as far as I know).
It seems that you are using WxWidgets framework but your compiler doesn't know where to find its headers, and apparently also libs which you would face with on a next step.
You, need to add to your compiler flags the output of wx-config --cxxflags. And also to your linker flags the output of wx-config --libs.
Assumption is of course that WxWidgets is installed on your PC

How to compile an application that uses libraries compiled from different compilers?

My question is as the topic.
I am currently using Mingw32 compiler in Qt creator to compile my application. The problem is that I include a .lib static library and header file which compiled from Visual Studio 2017 in my application.
When I further run or compiled my application in Qt. I would be facing the error code, unrecognized file format pointing to the .lib file.
I reasonably doubt that: For example, I can not use compiler 1 to compile other libraries compiled from compiler 2.
I follow the instruction here:libwdi Installation and Compilation to compile the "libwdi.lib" file.
FYI, the reason that I use Visual Studio to compile the .lib library(libWdi) is because it's easier to achieve on my Windows OS after I tried using wingw32-make from Qt5 Tool. It is such a pain since I can not even run ./autogen.sh to generate the makefile for Mingw32 on Windows.
Appreciate any step-by-step information on how to build a workable .lib file for Qt creator, including using VS, mingw, and cross-compilers.
If the target library uses c++ features in its public interface then it is likely this is not going to work regardless of what machinations you go through. Even different versions of the same compiler often have problems in that situation.
Pure C on the other hand will usually work (on Windows non-MS tool sets will be made to at least consume the MS intermediate object format, even if not used by the compiler/linker normally).

Compiling Fortran77 with Julia

I have a bunch of Fortran77 code that I need to use for my research but I'm having trouble compiling it to run on my MacBook so I turned to Julia. I'm new to the language but for the life of me I can't figure out how to execute a Fortran script directly in Julia. All I want is to have a program that runs a F77 script and hands control directly to Fortran. I would just rewrite it with Julia or Numpy but there's about 10,000 lines of code and less than 200 lines of comment and I don't have time for that.
It seems from the wording of your question like you want to use Julia to directly call Fortran "scripts" – presumably Fortran .f source files – is that accurate?
As others have indicated in the comments, Fortran is not a scripting language: you cannot directly execute Fortran source files; instead you must use a Fortran compiler (e.g. gfortran, ifort) to translate Fortran programs into native libraries or executables for the system you want to run programs on. Julia will not help with this in any way as Julia is not a Fortran interpreter or compiler – it can neither run Fortran code directly nor convert Fortran source files into executables/libraries.
If, however, you already have a Fortran shared library compiled (.so file on Linux, .dylib on macOS, .dll on Windows), you can call it easily from Julia, as described in Integrating Fortran code in Julia. If you can compile Fortran source code to an executable (as opposed to a shared library), then you do not need anything else to run it – executables, by definition, are standalone.
Most projects in compiled languages like Fortran or C/C++ come with Makefiles or other mechanisms to help invoke a compiler to generate the appropriate binary artifacts (executables and/or libraries).

Compile cpp source to run in c support only

I want to ask about a problem about c/c++ compile
I have project need to reuse a library wrote base on Cpp language.
But I need to reuse the library in a project which work on a platform only support c language (UEFI)
I want to ask :
-Is it possible to write a wrap API in C code to call all function in the Cpp library ?
-And After compiled All of these (Wrap API, CppLibrary,My Source project) by C compiler =>Will the build application is working in my platform (UEFI)? "
( Assumption than both Cpp library and My project source in compile in the same compile like (visual c 2013) )
Thank you
You can write a C API to wrap the C++ library, but if the C++ library can't compile and run in a UEFI environment, this won't do you any good.