Enabling curses in both Linux and Windows - c++

I am working on small project in C++ and I am using curses for user interface. I am pretty nicely able to make it work in my arch-linux installation, because it is pretty simple to set up ncurses to work there. But with my cmake setting which is working nicely at Linux I am not able to properly make it work at Windows.
Here is my CMakeList.txt
cmake_minimum_required(VERSION 3.9)
project(fighting_pit)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_CXX_STANDARD 11)
include_directories( ./include)
include_directories( ./src)
add_executable(fighting_pit
include/Arena.h
include/cursor.h
include/Player.h
include/spell.h
include/Turns.h
include/weapon.h
include/Draw.h
src/Arena.cpp
src/cursor.cpp
src/Player.cpp
src/spell.cpp
src/Turns.cpp
src/weapon.cpp
src/Draw.cpp
main.cpp )
target_link_libraries(fighting_pit ${CURSES_LIBRARIES})
I tried several approaches to make it work on Windows too.
1. Downloading sources
I tried to build pdcurses with mingw32-make. It created pdcurses.a I added it to same location as project, but it still shows it cannot find curses library.
2. Downloading via mingw32-get
I used installation manager from mingw and let it download both .dll and dev package of libpdcurses. Just trying to run cmake through clion showed it is still not found. So I copied it both into windows32 and project folder but it still didn't help.
I don't know what I should do. Unfortunately I am neither C++ user neither Windows user.

I needed to build a cross-platform project that uses ncurses on Linux and MacOS but uses pdcurses on Windows. Some variant of curses is usually installed on popular distributions of Linux. ncurses is also available on MacOS as well. The same isn't quite true for Windows. My solution was to download the pdcurses sources and write a cmake script for building it on Windows. if (WIN32 or MSVC) build pdcurses else() find ncurses. You might also want to create a proxy header that #includes pdcurses or ncurses depending on the platform.
After cloning the GitHub repo, I copied the headers in ., the C files in ./pdcurses, the sources in ./wincon into a new directory in my project. Then I wrote a CMakeLists.txt file to compile all of these files into a library. It looked something like this:
cmake_minimum_required(VERSION 3.2)
add_library(PDcurses
# all of the sources
addch.c
addchstr.c
addstr.c
attr.c
beep.c
# ...
)
target_include_directories(PDcurses
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
My main CMakeLists.txt file compiled the pdcurses sources if the target is windows.
if(WIN32 OR MSVC)
add_subdirectory(pdcurses)
target_link_libraries(MyTarget
PRIVATE
PDcurses
)
else()
# find ncurses and use that
endif()
PDCurses seems to be a (more or less) drop-in replacement for ncurses in most situations. I was able to compile the example programs that came with PDcurses on my Mac using curses without any troubles.

Related

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl

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.

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.

CMake on FreeBSD doesn't see GL/gl.h in /usr/local/include

I'm learning OpenGL and trying to make my code as portable as possible. For now I managed to compile a project on Ubuntu Linux 14.04, Windows 7 and MacOS. But I having some problems with FreeBSD (PC-BSD 10.2 if that matters). Here is a code example:
https://github.com/afiskon/cpp-opengl-model-view-projection
After running make (see all build steps in README.md) clang complains that it can't find <GL/gl.h> which is used in ./glfw/include/GLFW/glfw3.h. But GL/gl.h is present in /usr/local/include directory.
According to Google this is a well known issue. I tried to manualy edit CMAKE_CXX_FLAGS in CMakeLists.txt, modify environment varibales, etc. Nothing works.
Could you help me, please?
You do find_package(OpenGL REQUIRED) and use ${OPENGL_LIBRARY} (which should be either ${OPENGL_LIBRARIES} or ${OPENGL_gl_LIBRARY} according to documentation), but you don't do include_directories(${OPENGL_INCLUDE_DIR}).
FreeBSD installs all 3d-party software into /usr/local prefix, and many developers assume that all headers they need are within /usr. This is true for Linux just for coincidence. Because of that, if your software uses OpenGL, you should explicitly mention its include and library paths in your build system code and not make assumptions on their location.

zlib in Qt - QtZlib not present

I am using QuaZip library, which has zlib dependency. I want to compile my CMake managed application under Archlinux and Windows 7, in both I have Qt 5.3.0 installed.
On Linux:
I have read here "how to add zlib to an existing qt installation" that zlib is a native part of Qt installation. But in archlinux there is no such directory.
Of cause I searched through all other Qt include directories including QtCore, but there was no sign of zlib. On the other hand system installation of zlib can be found on archlinux through FindZLIB.cmake module.
On Windows:
In the Windows installation of Qt there is QtZlib folder in Qt include directory, so it can be included. Nevertheless, compiler always complain that he cannot link zlib functions from library, error log here. I've also tried to set external zlib library manually through TARGET_LINK_LIBRARIES but with no success.
Have anybody experiance with linking Zlib under Qt5 using CMake ?
Qt's zlib is an internal implementation detail. You're not supposed to use it. You need to link your own copy of zlib, just as you would need to if you weren't using Qt at all.
1) You should use your package manager on Archlinux and your own installation on Windows. Do not rely on the Qt third-party installation. It may be there today, but disappear at any certain moment when a new release comes out.
This is what I would suggest you doing on your Archlinux box:
pacman -S zlib
2) Also, you should use FindZLIB.cmake for finding zlib the following way in your CMakeLists.txt:
find_package( ZLIB REQUIRED )
if ( ZLIB_FOUND )
include_directories( ${ZLIB_INCLUDE_DIRS} )
target_link_libraries( YourProject ${ZLIB_LIBRARIES} )
endif( ZLIB_FOUND )