Compiling .cpp file using Cmake Software - c++

I want to create a binary file after compiling .cpp file and create a plug-in for a speech processing software (Praat).
However, this binary file creates compatibility issues on different platforms (Windows, Mac , Linux). I want to solve this by compiling the code using CMake.
Am I correct? I'm new to CMake. Could anyone provide any insight?

No, Cmake just help you in building process, clearly it makes Makefile's for you. you cannot compile a cpp file to a single (or plugable) binary file and expect that it could work on different platforms.
Also you could write your programs with Cross-Platform in mind, but they need to be compiled for each platform.
You could use platforms like Qt or Boost which help you write more Cross-Platform codes and compile them under different platforms without worrying about differences.
Also, The Qt has its own build system named QMake which is very easy to use and your use qmake instead of cmake to make a cross platform build system.
Also if you need truly cross platform binary programs, you could try things like Java.

Related

What is Cmake file and why do we use it when we have VIsual studio

Sorry for asking such basic question. Being a beginner in C++, I'm puzzled about a lot of things:
if we have Visual stuido and other IDEs, as well as Cygwin as compilers, what kind of roles do CMake play in helping a C++ programmer to develop programmes? Can we just write C++ programmes without using C Make?
I understand header files are like prototypes of functions that will be used in C++ files, or declarations of them, so I assume all header files should be accompanied by actual library files in the form of C++ files, as that's where the functions know what to do when being called. However,this is not usally the case, as there are header only libraries such as PyBind11.
What is bash? is it like another Windows command prompt? Whats its relationship wth C++?
Why do we have function macros and what is its advantages over normal functions?
Thank you very much!
CMake is a buildsystem.
if we have Visual stuido and other IDEs, as well as Cygwin as compilers, what kind of roles do CMake play in helping a C++ programmer to develop programmes? Can we just write C++ programmes without using C Make?
CMake allows for crossplatform development. It allows you to create projectfiles/Makefiles/... from a CMakeLists.txt file ==> The file suitable for the platform is created.
If you use CMake, you can simply program on windows (CMake will generate Visual Studio Solution Files) and if somebody tries to compile it on linux, CMake will generate Makefiles.
Using CMake, you only have one buildsystem and don't have to write buildfiles for every platform you support
I understand header files are like prototypes of functions that will be used in C++ files, or declarations of them, so I assume all header files should be accompanied by actual library files in the form of C++ files, as that's where the functions know what to do when being called. However,this is not usally the case, as there are header only libraries such as PyBind11.
I don't understand the question, but a header-only library hasn't to be linked to your program, as all the source code is in the headers.
What is bash? is it like another Windows command prompt? Whats its relationship wth C++?
bash is a shell, that executes commands. It's like the command prompt, only sometimes a bit better. It has no relationship to C++.
Why do we have function macros and what is its advantages over normal functions?
Macros have rarely advantages over functions, because the clutter the code, increase the compilation times (Because the preprocessor inserts a copy of the macro everytime, it is applied==>More code, slower compilation).
The only advantage I can think of, is in a situation like this for conditional compilation:
#ifdef DEBUG
#define LOG(x) printf(x)
#else
#define LOG(x)
#endif
what kind of roles do CMake play in helping a C++ programmer to develop programmes?
Can we just write C++ programmes without using C Make?
Yes. It is possible to write C++ programs without any tools. It is possible to even compile C++ without any build system.
However, building complex C++ programs without a build system is difficult and tedious. Build systems help to keep the complexity under control.
But using a platform specific build system hinders compilation on systems that don't have that specific build system.
CMake solves this dilemma by being able to generate builds for several build systems on various platforms. So CMake is a generator for build systems.
so I assume all header files should be accompanied by actual library files in the form of C++ files
Your assumption is not quite correct. It is a decent rule of thumb that applies often, but not always.
What is bash?
bash is a shell. It is the most commonly used shell on POSIX systems. On windows, bash can be used through WSL or Cygwin.
is it like another Windows command prompt?
Windows command prompt is another shell. It is used only on windows.
Whats its relationship wth C++?
There is no close relationship between bash and C++.
Bash is often used to write shell scripts. Such shell scripts are sometimes used to help with building complex C++ programs.
Why do we have function macros
For text replacement within the source code.
what is its advantages over normal functions?
Functions cannot replace text in source code.
Use cases for macros (functional or otherwise) are rare. They are sometimes overused, which is not a good thing. Generally, functions have many advantages over macros. Among the advantages of functions are type safety and scoped names.
CMake is a kind of a build system. Build systems help to organize all files in the project, handle proper order of compilation, linking, and other steps that are necessary to obtain a binary (executable or library). They allow describing the process of building in an abstract way, reducing dependencies between project and other build tools, e.g. you can easily change the compiler.
CMake is a special flavor of the build systems because it doesn't handle building by itself. It generates scripts of other build systems, e.g. Makefiles on Linux. That allows for a multiplatform approach to building applications.
Considering Your questions about macros and headers, I would suggest a good C++ book
Bash is a system shell with its own scripting language, allowing for running system scripts. It is mostly used on Linux based systems, however it can be used also on windows.

Better way to give provide path of libraries while compiling in C++

I pretty new to C++. I was wondering, as what is considered generally a neat way to provide paths for various files/libraries while compiling or executing c++ codes.
For ex:
I have Boost libraries installed in some location on my system. Lets call it X
In order to execute anything I have to type in
c++ -I LongpathWhichisX/to/boost_1_60_0 example.cpp -o example
Similarly, also Long path for the input file while executing the code.
Is there a better way to address it. Is it possible to create environment variables lets Y, which refers to path 'X'. And we can use following command to compile code
c++ -I Y/to/boost_1_60_0 example.cpp -o example
Best way is to use build tools. For example you can use Make. You define all your include paths (and other options) in the Makefile. In console you just have to call make to build your project or something like make run to run your project.
The usual way is to make a Makefile where you can specify all needed paths and compile options in proper variables.
If you don't want/need a Makefile and rather want to run compiler from command-line, then you may use the CPATH environment variable to specify a colon-separated list of paths to include files.
This is a broad question but the other answers highlight the most important step. It is essential to learn build tools like make because they will make it easier to build your projects during development and for others to build it later. In the modern programming age though this is not enough. If you are using something like Boost (which targets many platforms) you will probably want to make your build cross-platform as well. For this you would use either cmake or autotools which both have scripts that make it much easier to locate the Boost libraries (and others).
Any other build systems, in my opinion, are a pain and are the bane of maintainers of Linux distributions. CMake used to be in that catergory but it has gained wide acceptance now. CMake targets building cross-platform projects across operating systems (Windows and Unixes) better (again in my opinion) because it attempts to provide the native build system on each platform (for example: Visual Studio in Windows, Make on all Unices, XCode on Mac). The autotools instead target the Unix environment with much greater depth (you have a bit of a harder time on Windows, but you can target embedded Unix systems to high end Unix server systems with much more flexibility).
Note: Autotools support for cross-compiling is superior in almost every way to other solutions. I always cringe when I download something that needs to be cross compiled for Arm Linux and it uses some weird build system. Funnily enough, boost is one of these.
This is a bit of a long winded answer. In summary, it is essential that you learn a build system for native development. It is part of your skill set and until you have that skill you can't really contribute to open-source projects or even your employer developing closed-source projects.

Releasing a program

So I made a c++ console game. Now I'd like to "release" the game. I want to only give the .exe file and not the code. How do i go about this. I'd like to make sure it will run on all windows devices.
I used the following headers-
iostream
windows.h
MMSystem.h
conio.h
fstream
ctime
string
string.h
*I used namespace std
*i used code::blocks 13.12 with mingw
& I used the following library-
libwinmm.a
Thank you in advance
EDIT
There are many different ways of installing applications. You could go with an installer like Inno or just go with a regular ZIP file. Some programs can even be standalone by packaging all resources within the executable, but this is not an easy option to my knowledge for C++.
I suppose the most basic way is to create different builds for different architectures with static libraries and then find any other DLLs specific to that architecture and bundle it together in one folder. Supporting x86/x86-64/ARM should be enough for most purposes. I do know that LLVM/Clang and GCC should have extensive support for many architectures, and if need be, you should be able to download the source code of the libraries you use and then compile them for each architecture you plan to support as well as the compilation options you need to compile to each one.
A virtual machine can also be helpful for this cross-compilation and compatibility testing.
tldr; Get all the libraries you need in either static or dynamic (DLL) format. Check that they are of the right architecture (x86 programs/code will not run on MIPS and vice versa). Get all your resources. Get a virtual machine, and then test your program on it. Keep testing until all the dependency problems go away.
Note: when I did this, I actually had some compatibility issues with, of all things, MinGW-w64. Just a note; you may need some DLLs from MinGW, or, if you're using Cygwin, of course you need the Cygwin DLL. I don't know much about MSVC, but I would assume that even they have DLLs needed on some level if you decide to support an outdated Windows OS.

C++ Compile on different platforms

I am currently developing a C++ command line utility to be distributed as an open-source utility on Github. However, I want people who download the program to be able to easily compile and run the program on any platform (specifically Mac, Linux, and Windows) in as few steps as possible. Assuming only small changes have to be made to the code to make it compatible with the various platform-independent C++ compilers (g++ and win32), how can I do this? Are makefiles relevant?
My advice is, do not use make files, maintaining the files for big enougth projects is tedious and errors happen sometimes which you don't catch immediatly (because the *.o file is still there).
See this question here
Makefiles are indeed highly relevant. You may find that you need (at least) two different makefiles to compensate for the fact that you have different compilers.
It's hard to be specific about how you solve this, since it depends on how complex the project is. It may be easiest to write a script/batchfile, and just document "Use the command build.sh on Linux/Unix, and build.bat on Windows") - and then let the respective files deal with for example setting up the name of the compiler and flags, etc.
Or you can have an include into the makefile, which is determined by the architecture. Or different makefiles.
If the project is REALLY simple, it may be just enough to provide a basic makefile - but it's unlikely, as a compile of x.cpp on Linux/MacOS makes an object file is called x.o, on windows the object file is called x.obj. Libraries have different names, dll's have differnet names, and on Linux/MacOS, the final executable has no extension (typically) so it's called "myprog", where the executable under windows is called "myprog.exe".
These sorts of differences mean that the makefile needs to be different.

A single makeFile-Windows/Linux/Mac OS

We have some applications written in C/C++ and makefiles for the same. Currently, we are using the GNU make system on windows (cygwin based). The makefiles were written long back considering only Windows OS in mind. Now we are going to revamp everything.
I am unaware of the factors to be considered while writing the makefiles so as to make them cross platform compatible. I looked at some Sources on the Internet, but they were unsatisfactory. Can someone please list out the issues to be considered while writing the makefiles so as to make them compatible across various platforms.
PS: I have seen this link, but i think it isn't what i want.
Makefiles and cross platform development
You can use cmake - it's a cross platform tool which generates makefiles or projects files with respect to your platform. So instead of writing Makefile you write CMakeLists.txt, then you run cmake and it will generate Makefiles. When you want to compile your program on another platform you just ru-run cmake with different target project system.