How to run c++ Files using g++ and Cmake? - cmake

I am using Header Only Libraries. The libraries are included through Cmake. I am using the g++ Compiler.
Now what I am looking for is to compile the c++ source files via windows cmd. In this moment i am using clion to compile.
Does anyone know how to compile the source files?
If i am compiling the files without cmake , I am getting errors because the program cannot find the libraries.

You do not run a source file, you run an executable produced by a compiler from source file(s)
If you are under a Linux for instance enter the path(s) where the libraries are through the environment variable LD_LIBRARY_PATH
[edit after your remark]
I mean compile
To indicate to g++ where the library are to link use the option -L followed by the path of a directory where the libraries are. If your libraries are on different directories use several time the option, one per directory

Related

Build gtest as shared library (dll) in CMake

I have never worked with CMake before, so please forgive any rookie mistakes. Most of the following working frame has been given to me by my project group.
The goal is to build GoogleTest into a .dll, to be used in different, indepentent parts of our project. I'm having troubles setting up CMake the right way.
The work-flow so far has been:
Clone gtest from git --> also downloads a CMake List file
Alter variables in CMakeCache.txt to have it produce a Code::Blocks project file
Compile the project file in Code::Blocks
So far, it produces a static library (.a files) that can be used in our project. I'm having troubles genereating .dll files.
Variables I have tried changing:
BUILD_SHARED_LIBS:BOOL=ON --> the files generated by Code::Blocks now have a .dll.a double extension
CMAKE_C_FLAGS and all the corresponding C++ flags where set to -DGTEST_CREATE_SHARED_LIBRARY=1 as given here
CMAKE_EXE_LINKER_FLAGS has been set to -shared to make the linker produce .dll files
I have worked my way through the GoogleTest documentation here and here but in both, building it into a .dll is merely a 2-sentence-topic.
As #Tsyvarev pointed out, the .dll files were created in a (very) different folder.

Static library built with CMake as .a with Emscripten instead of .wasm + .js

TL;DR
How do I configure CMake and Emscripten to build my static library to produce a WASM and JS bootstrap file?
I have a static library being built with CMake that I want to build as a WASM library (and JS bootstrap) using Emscripten. Simply using the Emscripten CMake toolchain and adding the appropriate compiler/linker flags result in only a .a file being built - even if -o <project name>.js is added to the compiler and/or linker flags.
The reason is that because I've told CMake I want a static library, it uses CMAKE_AR to build. CMAKE_AR (if undefined) is defined as emar in the Emscripten toolchain file, and emar cannot produce .wasm and .js output.
I have tried creating a new executable target that has a dependency on the library, and otherwise just sets up the compiler/linker settings. However this causes a CMake error, because I've defined an executable target that has no source files (they're associated with the library target). If I add a stub main file, I get an Emscripten warning:
system_libs:WARNING: main() is in the input files, but "_main" is not in EXPORTED_FUNCTIONS, which means it may be eliminated as dead code. Export it if you want main() to run.
I could get round by adding an empty file to exe source file list (probably, I haven't tried), but this feels very much like a hack.
You are correct in that you need to create an executable target in order to produce a .wasm file.
If cmake insists on you creating a dummy source file because it doesn't understand that all the code for your program can come from libraries then I guess you that is your best option.
See CMake: Is it possible to build an executable from only static libraries and no source? for how to work around this limitation of cmake.

CMAKE - makefile target for a library

I'm currently changing the build system on my project, from gnu makefiles to cmake (that generate makefiles).
My project generates several libraries (.lib), and several executables (.exe).
Currently i generate the makefiles using the following command :
cmake -G "Unix Makefiles" .. -DCMAKE_BUILD_TYPE=Debug
The generated makefiles contain an all target, as well as a target for every executable (compiled as such with the add_executable cmake directive), so i can compile a subset of the project (which saves a lot of time) : make executable_1; make executable_2 and so on.
However, there is no target for the libraries (compiled as such with the add_library cmake directive) so i cannot do make library_1 for example. I want to do this because it would save a lot of time.
I tried to add a dummy executable in the library's cmake, and link the library to this executable (which only contains a main without actually using library_1's code).
add_library(library_1 test.cpp)
add_executable(dummy_exe dummy_main.cpp)
target_link_library(dummy_exe library_1)
It does add a target for dummy_exe but does not build the library because it does not actually need to link any of the library_1's code.
This was a workaround attempt anyway, and i'd rather just call make library_1 after all. Is there any way to add a makefile target for a library using cmake ?
As answered by w-m and Fred, CMAKE indeed create a target for libraries in the Makefile.
I was trying to build the library with the cmake subproject name of the library instead of the library name.
make help was indeed of big help to find this issue, as it lists everything that can be built.

Linking with ffmpeg shared build

I am trying to work with the ffmpeg library on a cygwin development environment. I downloaded the shared build from here (64-bit). The bin folders contain dlls but no .a or .dll.a files. Linking, which is done like so:
$ gcc encoding_mp3.o -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_shared/bin -lavcodec-54 -lm -luser32 -lpthread -o encoding_mp3
results in the following error:
/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_shared/bin/avcodec-54.dll: file not recognized: File format not recognized collect2: ld returned 1 exit status
My question(s):
1. The bin/ folder in the shared build contains only avcodec-54.dll. Is there a way to convert the dll to a dll.a or .a?
2. Where am I going wrong?
If source code is required, please let me know.
You need to download the "dev" versions, or the "static linking" ones (a little down, on the page).
dlls cannot be converted to libs, nor can they be used (usually) to generate they own little import stubs (the .dll.a)
From the page you linked "Dev packages provide the headers and .lib/.dll.a files required to use the .dll files in other programs."
.a / .lib usually are located in "lib" folders.

Can't find CMake created executable file

I am new to Cmake, I was trying to build an executble program from a Fortran code using Cmake-GUI. As per the program instruction I created the Cmakelists.txt. I then launched the Cmake-GUI and nominated the source directory and binaries directory and then used the configure and generate options to create my executable file. Everything seemed to have ran ok. Howover, when I checked my bin folder the .exe file that i was hoping to find is nowhere to be seen, instead I have cmake_Install.cmake and Makefile in the binaries folder. I opened these file with a text editor and they seem to be script file. Can somebody please tell me what I should do with these files to create the executale file. or if this is a sign of the cmake build process failing.
I don't know CMake for fortran, but cmake in general will create the makefiles / visual studio projects etc. We cmake into a bin directory, then go in there and run make. It doesn't build the source, it builds the make files.
Did you build the source?
CMake just create the makefiles and projects structures to compile using a make tool. I don't know how to compile it for Fortran, but in Linux for a C++ project, I just have to open a terminal, go to the build folder (your bin folder) and run make or make all and it compile the whole project.
I think that your problem is that your project is not compiled, so there is not any binary files.