So I've been wanting to learn C++, I have a student license for CLion and am familiar with other software from the company so I'd want to use it if possible. Using MSYS2 seemed like a good way to easily manage libraries, since that tends to be hellish anytime I tried working with C++.
MSYS2 seemed intuitive enough and I managed to install the OpenCV library as a test. However, I'm now entirely at a loss on how I'd link it with CLion.
I've been reading about CMake files, and this is what I figured should be mine:
cmake_minimum_required(VERSION 3.7)
project(letsee)
set(CMAKE_CXX_STANDARD 11)
find_package (OpenCV REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(letsee ${SOURCE_FILES})
target_link_libraries( letsee ${OpenCV_LIBS} )
The last line, however, is supposed to link to be an environment variable. I understand that MSYS2 should handle that somehow, or perhaps I should create my own windows environment variable? Either way I'm not even sure to where I'd link such variable. I'm just incredibly confused by this point. How can no one have created an intuitive way to handle this in windows in a 40 year old language.
I just downloaded and setup everything to try it. Here is how it works:
Install MSYS2 and follow the tutorial on their website (pacman -Syu, pacman -Su) - you probably did that already
pacman -S mingw-w64-x86_64-toolchain (you probably did this too)
pacman -S mingw-w64-x86_64-cmake This is the important step. We will use this CMake instead of the bundled one, because this CMake works with MSYS2 pacman libraries
Configure CLion: MinGW: C:\msys64\mingw64 (or similiar), CMake: C:\msys64\mingw64\bin\cmake.exe
CLion might warn you because CMake/GDB are too new. However, I haven't experienced any problems until now
Edit: I actually also tested it with the bundled CMake right now and this worked too, out of the box. So no idea why it doesn't for you.
Related
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.
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.
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})
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.
I'm new to CMake and to Boost!
I'm on a mac, so I can't test Boost code on my machine. I have a VM with Ubuntu. I am using JetBrains CLion IDE, which uses CMake to build. I've been unsuccessful with every tutorial I have tried. So, I just decided to make a project that does nothing. I've added the Boost information to the CMakeLists.txt file:
EDIT
Since I really didn't understand the CMakeLists.txt configuration, I learned that I was using one wrong configuration and needed to just remove the set(Boost_LIBRARY_DIR /usr/local/arm/lib) command. Here is a new image of the CORRECT CMakeLists.txt file.
I can't find where Boost_LIBRARIES is getting configured, which might have something to do with my problem. You can see main does nothing but print out "Hello, World!"
Here is the error I get when I try to build:
Boost_LIBRARIES is configured when you call find_package(Boost 1.54 ....)
The line
set (Boost_LIBRARY_DIR /usr/local/arm/lib)
has no impact in your code.
The mac still uses the intel processor, AFAIK, and so would your Ubuntu VM. From the location of your boost libraries it looks like boost was compiled for arm. Do you have other boost libraries installed on your system? Did you install boost yourself?
a