Matlab C++ S-Function with multiple source files - c++

I have a simulink model with a c++ s-function.
This s-function needs access to a lot of (>50) classes. Each class is consists of a header (.h) and a source (.cpp) file.
I have also divided my code into multiple directories:
root
-sfun.cpp
-folder1
--file1.h
--file1.cpp
--file2.h
--file2.cpp
-folder2
--file3.h
--file3.cpp
...
For the compilation of the s-function I am using the mex-function:
mex -Ifolder1 -Ifolder2 -outdir bin sfun.cpp folder1/file1.cpp folder1/file2.cpp folder1/file3.cpp
(http://de.mathworks.com/help/matlab/ref/mex.html)
But this gets very long and ugly with more and more files because I need to specify each header folder and earch source file separately.
Is there a better way to create a mex file that needs access to lots of source files?
I have the following ideas, but I am not sure what could be the correct and easiest way:
Add all header and source files (fileX.h/ fileX.cpp) to an visual studio project and compile them to a *.lib file. Then compile only the sfun.cpp with the mex tool and provide access to the *.lib file
Move all header and source files into one directory. This would shorten the command line as follows:
mex -outdir bin sfun.cpp file1.cpp file2.cpp file3.cpp
Making everything inline so that there is no need for a source file. (very ugly solution)
Is there some kind of a makefile for the mex compiler?
Include not only the header files but also the source files via a #include directive.
At the moment I am not convinced of any of these ideas and I would appreciate any help.
Thanks
Edit1:
One annotation: This project should be ported to a dspace pc in a later stage. Do I need to consider something special in this case?

Related

How to include .cpp and .hpp files of a library in my c++ project in visual studio

Apologies for a very basic question. I am trying to port from a CMake project into a visual studio project.
Basically I want to run the project from visual studio without using the CMake file. In the project I need to port, there are many folders and sub folders that contains many .cpp and .h files. These are included to the main cpp file as using #includes.
My Case
The library I want to include
and my main.cpp
For instance to make things simple assume I have a main file main.cpp and this file includes #include "Libpfs/colorspace/colorspace.h". The Libpfs is a folder and it has many sub folders one of which is colorspace folder and this has many .h and .cpp files. One .h file is colorspace.h that is included in main.cpp using the #include and the folder also has .cpp file i.e. colorspace.cpp.
My Attempt
My objective is include them to my project.
Now here is what I have tried
in Visual Studio Project->properties->C/C++ in Additional Include Directories I gave the path of the folder that contains Libpfs but this approach did not work and gave linker errors this might be because I have no lib files for the Libpfs (correct me if I am wrong). I only have .cpp files of the corresponding .h files. I presume the cause of error is the the .cpp files are not compiled yet.
My Question
How can I include the cpp file to my project as well (not the lib files since I dont have those).
Using this for a source, I find the CMakeLists.txt to contain nothing special.
FILE(GLOB COLORSPACE_H *.h)
FILE(GLOB COLORSPACE_HXX *.hxx)
FILE(GLOB COLORSPACE_CPP *.cpp)
SET(LIBPFS_H ${LIBPFS_H} ${COLORSPACE_H} ${COLORSPACE_HXX} PARENT_SCOPE)
SET(LIBPFS_CPP ${LIBPFS_CPP} ${COLORSPACE_CPP} PARENT_SCOPE)
So you can just add all the files to a VS C++ project. I would use some directory management, to separate these sources from your own.
But anyhow, in that case, you should include the sources by relative path. E.g.
#include "../../Libpfs/colorspace/colorspace.h"
Alternatively, you could put everything in a separate C++ library (static .lib or dynamic .dll). In that case you should but the binaries in in a bin path and add that as additional library directory (project properties of your own project) and put all the header files in an include/Libpfs path and add that as additional include directory. In that case you should include the files as.
#include <Libpfs/colorspace/colorspace.h>
On another topic
#define pow_F(a,b) (xexpf(b*xlogf(a)))
I found this define only in the sources of the same source used above.
It seems to be sourced from sleef and according to this it should give a speedup. But you should measure if that is really still true, instead of doing copy-paste/cargo cult programming.
I think generally you should use the standard library std::pow, which has overloads for float, double and long double. The compiler will in most cases optimize its use for you.
open explorer , look for C:\Program Files\Microsoft SDK\ and then go after dir with alot of .lib's in its \lib .
it's an msvc source for .lib and other stuff like that . copy your library there

CMake add_executable() use .cpp file or .hpp file?

I've read a bunch questions on this site about the usage of add_executable function in CMake, but have not found an exact answer to my confusion.
My question is why we only add .cpp files in the add_executable function but not .hpp files?
I understand header files are like "indices" for functions and classes in the corresponding .cpp files. But if we don't include them in the add_executable function, how are they used in the build process?
For example, will the A.hpp file be used when another source file import A.hpp? But then A.hpp is not in the add_executable function... How does the program know where to find A.hpp?
Thanks!
Header files, which often have .h or .hpp extension, although not always - for example, C++ standard library headers have no extensions - are "copy-pasted" by compiler into every .cpp (or .C, or .cc) which has #include directive to include the file.
Because of that, build system, such as CMake, doesn't have to know about them when the final executable is built - their contents is already accounted for by literal inclusion of their code into .cpp file.
However, build systems need to know about those files when dependencies are specified - to ensure that the whole application is rebuilt whenever any of those files is updated, and also to provide the proper inclusion path to the compilation command.

What is the difference between a cbp file and a cpp file?

In code blocks their default file is main.cbp so I usually change it to main.cpp. But there doesn't seem to be a difference between their performances. But then again I just began coding in C++ so I'd like to know if there are any differences before I get too deep.
.cbp is the extension for a codeblocks solution file. Usually the project file will contain the .cpp file. chp files dont contain the actual source code but the procedure for codeblocks to associate files.
In a nutshell, .cpp contains the source code while cbp files dont.
Why do you have to know about that?
Answer: when passing source code, cpp file is the only format that can be opened for IDE other than codeblocks eg. Dev C++

How do I tell Visual Studio to include a library's .cpp file?

I am using lodepng to produce compressed image files. I have stored the .cpp and .h files (there is only 1 of each) in C:/Program Files/Common Files/lodepng/lodepng, and I want my visual studio project to automatically look in these folders.
I have added C:/Program Files/Common Files/lodepng to my Include Directories, so wherever I have #include <lodepng/lodepng.h> it not picks up that header file, which is nice.
However, when I try to compile I get a linker error (obviously) because I haven't told VS where to look for the .cpp file (it's in C:/Program Files/Common Files/lodepng/lodepng/lodepng.cpp). But how do I tell it that?
Currently the fix I'm using is to simply drag and drop loadpng.cpp into my Source Files of the project, but this isn't a very nice solution.
Any ideas?
You have to add your .cpp files to (compile with) your project.
Right click the project in the solution explorer, then select Add -> existing item and select your .cpp files. Note the folder the files live must be inlcuded to your diretories
VC++ would only look files for you when encountering directives like:
#include "lodepng.h"
Though possible it is not a common practice to include a cpp file, in fact you could include text file for example to include very large data file etc.
Best practice is to add cpp file into your project, don't put any cpp file in a header folder and don't include them unless there are no any alternatives.

including header file in a separate folder

I created a class (say, myclass.h/cpp). I want to use the class from many different places. Therefore, I put those files in a folder (say, C:\cpp_include) and I want to include them from whatever folder my codes are. I have a code which uses the class (say, main.cpp). In main.cpp, I include myclass:
#include "myclass.h"
I compile using a .pro file and nmake. In the .pro file, I specify the folder as:
INCLUDEPATH += C:\cpp_include
When I compile the code using nmake, myclass.h is properly included, but myclass.cpp doesn't seem to be found by compiler.
When I specify myclass.cpp as one of the source files in .pro file:
SOURCES += main.cpp C:\cpp_include\myclass.cpp
The exe file is built correctly. But, I would like myclass.cpp file to be found automatically when myclass.h is included, i.e. without setting myclass.cpp as a source file. Would this be possible? It looks like that's what happens with classes from Qt and Qwt (e.g .h/cpp files in /src/ folder in Qt and Qwt). Am I missing somthing?
Thanks a lot!
Daisuke
A simple technique is to have build scripts (makefiles) in the cpp directories. Write a rule that traverses the directories, executing the build scripts. This one step in isolating functionality and also allows one to use libraries.
That's just not how it works. The .cpp is the file that matters, header files (.h) just get copied into the other .cpp files. Therefore you need to add the myclass.cpp to your sources for compiling. Or, if it's a library class, you could also compile it once into a static library (.lib) and just add that to your linker files. But you ultimately need to somehow include you implementation in the project where it's used.