CMake on Windows 10 doesn't generate executable - c++

So I have a tiny C++ project that I code on both Windows 10 and Ubuntu 16.04, and I'm using CMake to coordinate relatively platform-independent build processes. I'm no master of CMake, but it worked up until recently, however now I cannot get the solution generated by CMake-gui to actually compile into an executable to run, despite my usage of add_executable. Here is my CMakeLists (there's a hideous mix of platform-specific stuff in there, however it appears that CMake simply ignores it):
cmake_minimum_required(VERSION 3.10)
set (CMAKE_CXX_STANDARD 17)
project (TestProject)
include_directories(C:/include/CImg-2.2.2)
ADD_LIBRARY(LibsModule Game.cpp Game.h TestGame.cpp TestGame.h)
target_link_libraries(LibsModule -lpthread)
target_link_libraries(LibsModule -lm)
target_link_libraries(LibsModule -lX11)
add_executable(TestProject TestGame.h)
target_link_libraries(TestProject LibsModule)
This compiles and runs fine on Ubuntu using CLion, however on 64-bit Windows 10 using VS17 it fails upon trying to run the executable it expects to be generated because the executable doesn't exist in the build folder (but the .lib file noted is generated just fine).
I've seen some mention that a third party antivirus program could cause this, but I have no such thing installed.
Now I'm pretty new to CMake so if this file is completely off the mark I would really appreciate a clear explanation as to what I'm doing wrong, this code is made from a short study of basic CMake usage.

Related

Static Compilation for wxWidgets C++

As a starter I would like to point out that I have spent few hours trying to resolve the problem with no success. I wrote a small project using wxWidgets library for C++ in Clion on Mac. Since majority of my classmates have done their projects in VisualStudio on Windows, nobody could help me out. The project must be handed in in a form of an executable which will work on any computer. I suppose that the solution I should be looking for is static compilation which would staticly link all of the libraries to the executable file. I tried adding some flags like the one below to the Cmake:
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
but it did not help whatsoever. I have absolutely no knowledge on how CMake works and I would greatly appreciate any help.
The whole cmake file which I currently have:
cmake_minimum_required(VERSION 3.9)
project(Lab3)
set(EXECUTABLE_NAME "Indexer")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra -DWX_PRECOMP")
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
include_directories(include/ ${wxWidgets_INCLUDE_DIRS})
file(GLOB SOURCES "src/*.cpp")
add_executable(${PROJECT_NAME} main.cpp Source.cpp Source.h)
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
EDIT: I guess I didn't specify the problem clearly enough. The program compiles fine- everything works. It's just that whenever I try to copy the executable file (the end application) to a different computer it just doesn't work or shows up as a text file with corrupted characters instead of an application with a GUI etc..
You're not copying the right thing across. What you believe to be the application is probably just some kind of launcher.
Mac GUI applications do not consist of just a single file. Your Indexer app is actually what Apple call a bundle. The Finder presents this to the user as a single entity but it's really a folder.
You can look inside a bundle by right-clicking on it (or Ctrl+click) and selecting Show package contents. If you want to understand a bit more about what's in there, Apple document it here.
A good way to get a bundle from A to B might be to ZIP it. Once you have done that you will find out whether your efforts to statically link it have been successful. What I don't know is where your build system has put it, but maybe this post will help. It will be called Indexer.app, maybe Spotlight can find it.
To build wxWidgets with as few dependencies as possible, you should configure it with not only --disable-shared, but also --disable-sys-libs, to prevent it from using any third party libraries (e.g. libz, libpng, ...) that might be installed on your system.
After building your application using wxWidgets, you should confirm that otool -L YourApp.app/Contents/MacOS/YourApp doesn't show any non-system libraries.

ITK: Can't find ITKCommon-x.xx.dll when executing HelloWorld Example

i successfully built ITK 4.13 (as 'Release') on Windows 10 with the latest Visual Studio version (Visual Studio 15 2017 Win64 compiler). Then i tried to built the ITKHelloWorld example using the following CMakeLists.txt (as shown in the example by ITK):
#CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(HelloWorld HelloWorld.cxx)
target_link_libraries(HelloWorld ${ITK_LIBRARIES})
Built runs without errors or warnings. Running the generated HelloWorld.exe file result in an ITKCommon-4.13.dll not found error.
The ITKCommon-4.13.dll was built by ITK, thus, i think i have to change something in the CMakeList file, but don't know what.
Any guess or solution?
Edit: The include(${ITK_USE_FILE}) expression in CMakeLists searches for UseITK.cmake, but this file is located in the source of ITK not in the build folder. Could this be a source of the error?
Edit2: Running a VTK Example also shows errors caused by not finding required .dlls.
There is no problem with your project. Windows can use dll libraries on runtime only if they are in PATH location. So there are two options: either you add bin/Release directory of ITK library to Windows's PATH system variable (the directory that contains all the ddls), or you just copy all the dlls from that directory to the one that contains your new application.
Precisely, you don't need to copy all the dlls, you can check by e.g. Dependency Walker, which ones are really needed.

Compile Linux OpenGL Application on Windows?

I have made a OpenGL application which I can compile fine on Linux. It uses static .a librarys. Now I tried compiling with MinGW (and .lib libraries) and g++, but the resulting main.exe displays this error when I try to execute it: https://i.imgur.com/dpidmsw.png.
g++.exe -std=c++11 -Wall -o main.exe Main.cpp FastNoise.cpp shader.cpp texture.cpp glew32.lib glfw3.dll -lglu32 -lopengl32
When using cmake with the CMakeGUI to create a Visual Studio Project it works fine:
cmake_minimum_required(VERSION 3.5)
add_definitions(-DGLEW_STATIC)
set (CMAKE_CXX_STANDARD 11)
add_executable(main Main.cpp texture.cpp shader.cpp FastNoise.cpp)
find_package(OpenGL REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} )
IF (WIN32)
target_link_libraries(main ${CMAKE_CURRENT_SOURCE_DIR}/GLEW_1130.lib ${CMAKE_CURRENT_SOURCE_DIR}/glfw3.lib ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${X11_LIBRARIES})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/vertex.glsl ${CMAKE_CURRENT_BINARY_DIR}/Debug/vertex.glsl COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/fragment.glsl ${CMAKE_CURRENT_BINARY_DIR}/Debug/fragment.glsl COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/dirt.bmp ${CMAKE_CURRENT_BINARY_DIR}/Debug/dirt.bmp COPYONLY)
ELSE()
include_directories( ${X11_INCLUDE_DIRS} )
find_package(X11 REQUIRED)
target_link_libraries(main ${CMAKE_CURRENT_SOURCE_DIR}/libGLEW.a ${CMAKE_CURRENT_SOURCE_DIR}/libglfw3.a ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${X11_LIBRARIES} X11 Xxf86vm pthread dl Xrandr Xinerama Xcursor)
ENDIF()
Just because you have static libraries that doesn't mean that these are in any way system independent. The static libraries you have are all built for a Linux target. They expect to talk with Linux operating system functions using the a calling convention (i.e. the way functions are to be called) that's different from Windows. Essentially you're trying to shove a square peg through a round hole here. It doesn't work that way.
To build for Windows you need also all the prerequisites being ported and built for Windows, too. Luckily all the libraries you mention have Windows ports, so that's not a problem.
I fixed it by adding these praeprocessor definitions:
…
You fixed nothing. You just lied to something (compiler, your program source code, etc.) about the toolchain it's built with and the target it's built for.
If you start redefining system level preprocessor definitions, you're doing something very, very wrong.
Here's what you should do (well, what I recommend): Head over to http://www.msys2.org/ and grab the MSys2 installer. Install it, launch the MinGW… environment (there's a 32 bit and a 64 bit environment). MSys2 uses the Pacman package manager of the Arch Linux distribution. Use that to install the toolchain (make, binutils, gcc) – make sure you install the right variant, the package database has packages for msys, mingw32 and mingw64 – and all the development libraries you need. There are packages for GLEW, GLFW3 and so on available.
Next install CMake. I strongly recommend not to use the MSys2 package, but the standalone installer from https://cmake.org/ – the CMake installed this way knows how to work with MSys/MinGW …and more.
Then create a CMakeLists.txt for your project, and use that to create the build environment. The nice thing about CMake is, that it's cross platform and knows a ton of build systems and compiler environments. If your project is structured sanely and you don't do crazy stuff (or you do crazy stuff and put the right straightjackets around it so it doesn't go on a rampage) you'll get something that you can build effortlessly on or for most target environments.

How to use pkg-config in CMake (juCi++)

I've been happily programming in C++ and compiling with g++ for quite a while. Not long ago, I'd decided to get an IDE, and I came accross juCi++.
This IDE is absolutely brilliant, but it uses CMake (or Meson) to build projects. This wasn't a problem, until I had to include a library (GTK+ 3.0 if you're wondering) using pkg-config. This could be done quite easily when compiling with g++, but, as I am completely new to CMake, I have no idea how to do it in the new IDE.
Can somebody please explain?
If your IDE handles CMake and Meson, it should be able to detect your project files. I'd say go for Meson, it's the future, and CMake syntax has a few quirks that Meson doesn't.
Meson:
Meson documentation
He's a basic meson.build that expects to find your application code in main.c and produces a binary named gtk3-test.
project('gtk3-test', 'c')
cc = meson.get_compiler('c')
deps = dependency ('gtk+-3.0')
sources = ['main.c']
executable('gtk3-test', sources, dependencies: deps)
CMake
CMake documentation
For CMake, just give a look at my answer to How do I link gtk library more easily with cmake in windows? (which also works under Linux). It was for GTK+2, but adapting it to GTK+3 is easy, so here's the CMakeLists.txt to use:
project (gtk3-test)
cmake_minimum_required (VERSION 2.4)
find_package (PkgConfig REQUIRED)
pkg_check_modules (GTK3 REQUIRED gtk+-3.0)
include_directories (${GTK3_INCLUDE_DIRS})
link_directories (${GTK3_LIBRARY_DIRS})
add_executable (gtk3-test main.c)
add_definitions (${GTK3_CFLAGS_OTHER})
target_link_libraries (gtk3-test ${GTK3_LIBRARIES})

Library linking in C++/Qt distribution

I am developing a project in C++11, and am not the biggest expert in distrubition, compiling, packaging, etc. I use
CMake 3.1.0
Qt 5.4 (community edition)
Moreover, I want the program to work on both OS X and Windows. Therefore, my IDEs include:
Apple Xcode 7
Microsoft Visual Studio 2013
I am having troubles with the Windows build. As of now, I am not able to copy the executables to another computer, since the DLL-files are not included. If I copy all the DLL-files that I receive errors from, it simply says the program was not able to run. I have read a bit about static library linking, but am unsure whether this is what I need.
My CMakeLists.txt looks like this:
set(libsources file1.cpp file2.cpp)
set(exec1sources file3.cpp file4.cpp)
set(exec2sources file5.cpp file6.cpp)
# some qt commands
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ..)
add_library(sharedlib ${libsources])
generate_export_header(sharedlib)
add_executable(exec1 ${exec1sources})
add_executable(exec2 ${exec2sources})
target_link_libraries(sharedlib Qt5::A Qt5::B)
target_link_libraries(exec1 Qt5::A Qt5::B sharedlib)
target_link_libraries(exec2 Qt5::A Qt5::C sharedlib)
install(TARGETS exec1 RUNTIME DESTINATION bin)
install(TARGETS exec2 RUNTIME DESTINATION bin)
As you can probably see, I want 2 executables, that each use some functionality from a shared library. And I want them working stand-alone.
I am likely not the first in the world to have this issue, but I was unable to find a similar thread on StackOverflow.
Have you tried windeployqt? It will automatically copy all dependencies for you application. You can find it in the bin-folder of your Qt-Kit
For Mac, there is a similar tool, called macdeployqt.