Using wxWidgets with CLion -- SOME ANSWERS (2020)

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
User avatar
conleec
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles
Contact:

Using wxWidgets with CLion -- SOME ANSWERS (2020)

Post by conleec »

Hello everybody,

CLion uses a CMake based build system, and there is a bunch of confusing, contradictory, outdated, and flat-out wrong information out there about how to use CLion with the wxWidgets library.

Here's what worked for me, step by step, for anybody who's interested.

1) I downloaded the latest build (3.1.1) and unzipped it.
2) I moved the unZipped folder into my Documents folder (/Users/Documents/wxWidgets-3.1.1 in my case)
3) Also inside the Documents folder I created a new folder called wxWidgets-Debug

This is the folder into which we will build the library, and link to it.

4) Open a terminal window and cd into the wxWidgets-Debug folder (cd ~/Documents/wxWidgets-Debug in my case)

5) Inside this folder type: ../wxWidgets-3.1.1/configure --disable-shared --enable-debug --enable-stl --with-cxx=11 --with-macosx-version-min=10.12 --prefix="$(pwd)"

A few notes about this: The ../wxWidgets-3.1.1/configure command is telling the system to step out of your build directory and INTO the source directory that you downloaded and unZipped in order to run the configure script. If you place your build directory differently, you'll have to modify this.

Secondly, the --with-cxx=11 option tells the system to build using C++11, which I needed to do to eliminate some warnings I was getting.

Thirdly, the --prefix="$(pwd)" option instructs the library to be INSTALLED into Present Working Directory (ie pwd). I had to read up on BASH scripting to get what this all meant.

5) Once the configuration is complete, I typed: make -j8

This is the step where the system will compile the library, using the configuration options given in the step above. The -j8 options tells the system to use 8 processes. I've read you shouldn't make this number larger than the number of physical cores you have. Therefore, if you have 4 cores, use -j4, etc. It's optional, but it definitely speeds up the build process.

6) This is a long process and the library is built. Keep an eye out for errors, etc. On my system, using the configuration above, I had no errors, but did get some warning about an audio toolkit. Haven't squashed that one yet, but for what I'm doing it doesn't seem to hurt anything. If anybody has any ideas, I'm all ears.

7) Check what version you have installed by typing: ./wx-config --version

At the time of this writing, I'd installed 3.1.1 and indeed, this command returns 3.1.1, so we're good to go.

Well, now for the part we've all been waiting for! Open CLion and select "Import Project" and navigate to the Samples folder in the SOURCE directory (this is the original directory that you downloaded, NOT the build directory that you just built the library into). Select the "minimal" folder and hit OK.

You should get a dialog asking if you want to Import as a new CMake project, Cancel or Open Existing Project.

Select Open Existing Project.

You should now see something like this:

Code: Select all

#############################################################################
# Name:        samples/minimal/CMakeListst.txt
# Purpose:     Sample CMake file to show usage of cmake for wxWidgets users
# Author:      Tobias Taschner
# Created:     2016-10-23
# Copyright:   (c) 2016 wxWidgets development team
# Licence:     wxWindows licence
#############################################################################

#
# This file is just a sample to show using cmake from an application
# If you want to build the minimal and other samples please use the
# wxBUILD_SAMPLES option when using cmake on the library
#

# Declare the minimum required CMake version
cmake_minimum_required(VERSION 3.12)

# Name the project
project(minimal)

# Request the required wxWidgets libs
find_package(wxWidgets 3.1 COMPONENTS core base REQUIRED)

# Include the wxWidgets use file to initialize various settings
include(${wxWidgets_USE_FILE})

# Define a variable containing a list of source files for the project
set(SRC_FILES
    minimal.cpp
    )

if(WIN32)
    # Include a RC file for windows
    list(APPEND SRC_FILES ../sample.rc)
elseif(APPLE)
    # Add an icon for the apple .app file
    list(APPEND SRC_FILES ../../src/osx/carbon/wxmac.icns)
endif()

# Define the build target for the executable
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ${SRC_FILES})
# Link required libraries to the executable
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

if(APPLE)
    set_target_properties(${PROJECT_NAME} PROPERTIES
        RESOURCE "../../src/osx/carbon/wxmac.icns"
        MACOSX_BUNDLE_ICON_FILE wxmac.icns
        MACOSX_BUNDLE_COPYRIGHT "Copyright wxWidgets"
        MACOSX_BUNDLE_GUI_IDENTIFIER "org.wxwidgets.minimal"
        )
endif()
You will also most likely see the CMake log window open and you'll see an error that CMake could NOT find wxWidgets.

HERE'S THE MAGIC INGREDIENT

In order to tell CLion where to find the wxWidgets library, you have to open CLion Preferences, then step into Build, Execution, Deployment.

Then click on CMake. You will then be presented with some CMake settings. The one we're interested in is the Environment: section.

Click on the folder on the right side of the text entry field and it will present you with a window for adding environment variables to your CMake session.

Click the + key at the bottom.

You'll see a window for entering Names and Values.

In the left column add: CMAKE_PREFIX_PATH

And in the right column, paste in the full path to your BUILD DIRECTORY, in my case: /Users/mediacomposer/Documents/wxWidgets-Debug

Once this is done, you should be able to click the little gear icon to the left of CLion's CMake log window and clear cache and reload project. If all has gone well, the error message will have disappeared, and you can now build the minimal project by clicking the little Hammer icon at the top of your CLion screen.

It should build without errors. You can then find the executable by navigating to the "minimal" folder and then one step further into the folder named: cmake-build-debug.

Double-click the icon and the minimal project will run.

YAY!!!! You wouldn't believe how many weeks it took me to get just this far.

But it gets even better.

By using this basic CMake file, you can step into any of the other samples and make slight modifications and build them all, using CLion.

Generally speaking, you "import" whatever sample you want, accepting the defaults that CLion present you with. It will then build a basic CMake file, which will generally just have the add_executable command with a list of file(s). It will not, however, compile.

To make it do so, copy over from the "minimal" CMake file this portion:

Code: Select all

# Request the required wxWidgets libs
find_package(wxWidgets 3.1 COMPONENTS core base REQUIRED)

# Include the wxWidgets use file to initialize various settings
include(${wxWidgets_USE_FILE})

# Define a variable containing a list of source files for the project
set(SRC_FILES
    minimal.cpp
    )

if(WIN32)
    # Include a RC file for windows
    list(APPEND SRC_FILES ../sample.rc)
elseif(APPLE)
    # Add an icon for the apple .app file
    list(APPEND SRC_FILES ../../src/osx/carbon/wxmac.icns)
endif()

# Define the build target for the executable
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ${SRC_FILES})
# Link required libraries to the executable
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

if(APPLE)
    set_target_properties(${PROJECT_NAME} PROPERTIES
        RESOURCE "../../src/osx/carbon/wxmac.icns"
        MACOSX_BUNDLE_ICON_FILE wxmac.icns
        MACOSX_BUNDLE_COPYRIGHT "Copyright wxWidgets"
        MACOSX_BUNDLE_GUI_IDENTIFIER "org.wxwidgets.minimal"
        )
endif()
Paste it into the current CMake file, right after the project(name) line.

Now, in order to make this work with all the samples, in the find_package command, right after COMPONENTS, where it currently says "core base", replace this with "all". I surrounded this with quotes, just to make it easier to read, but you won't actually use quotes. This tells CMake to use ALL the wxWidgets libraries. This is undoubtedly NOT GOOD PRACTICE, but it works, and as you and I learn more we can learn how to only use the libraries we actually NEED instead of all of them.

To summarize, this line will now read: find_package(wxWidgets 3.1 COMPONENTS all REQUIRED)

Next, copy the list of file names from the current CMake file's add_executable list and paste them into the set(SRC_FILES) list. Only copy the .cpp and .h files, if they exist.

For instance, if importing the "calendar" sample, copy calendar.cpp and paste it into set(SRC_LIST calendar.cpp). If importing the "dataview" sample, copy dataview.cpp, mymodels.cpp and mymodels.h like so: set(SRC_LIST dataview.cpp mymodels.cpp mymodels.h)

Hopefully this will make more sense when you actually DO IT.

Lastly, if using a mac, then in the last line of the if(APPLE) block, change the bundle_gui_identifier to be "org.wxwidgets.projectname"

Using "dataview" sample as an example, the CMake file should now look something like:

Code: Select all

cmake_minimum_required(VERSION 3.12)
project(dataview)

set(CMAKE_CXX_STANDARD 14)

# Request the required wxWidgets libs
find_package(wxWidgets 3.1 COMPONENTS all REQUIRED)

# Include the wxWidgets use file to initialize various settings
include(${wxWidgets_USE_FILE})

# Define a variable containing a list of source files for the project
set(SRC_FILES
        dataview.cpp
        mymodels.cpp
        mymodels.h
        )

if(WIN32)
    # Include a RC file for windows
    list(APPEND SRC_FILES ../sample.rc)
elseif(APPLE)
    # Add an icon for the apple .app file
    list(APPEND SRC_FILES ../../src/osx/carbon/wxmac.icns)
endif()

# Define the build target for the executable
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ${SRC_FILES})
# Link required libraries to the executable
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})

if(APPLE)
    set_target_properties(${PROJECT_NAME} PROPERTIES
            RESOURCE "../../src/osx/carbon/wxmac.icns"
            MACOSX_BUNDLE_ICON_FILE wxmac.icns
            MACOSX_BUNDLE_COPYRIGHT "Copyright wxWidgets"
            MACOSX_BUNDLE_GUI_IDENTIFIER "org.wxwidgets.dataview"
            )
endif()
Don't forget to set the CMAKE_PREFIX_PATH environment variable to your wxWidgets build directory, as discussed above!

This should allow you to build the sample and run it.

There is only ONE OTHER THING to be aware of.

If the sample has any graphics, icons, or supporting items in the root directory, those items need to be copied into the new cmake-build-debug folder of the sample's directory, or else the system won't know where to find them.

That's it!

I really hope this helps somebody, and saves the many many hours I had to spend to get something up and running with CLion.
Last edited by conleec on Sat Feb 22, 2020 1:34 am, edited 2 times in total.
cagansevencan
In need of some credit
In need of some credit
Posts: 1
Joined: Mon Apr 15, 2019 9:08 pm

Re: Using wxWidgets with CLion -- SOME ANSWERS (2018)

Post by cagansevencan »

Sir, you have saved my life. I really wouldn't want to deal with slow Ubuntu system whereas I can code in peace with Clion. THANK YOU SO MUCH!
User avatar
conleec
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles
Contact:

Re: Using wxWidgets with CLion -- SOME ANSWERS (2018)

Post by conleec »

Wow, so glad it has helped someone. I've been so busy at work, that I haven't been able to do any coding since I posted this. Had almost forgotten that I did post it, to be honest. Hope to get back to practicing in the next month or two, and I'll have to re-read what I did to get myself going again. :wink:
---------------------------
Chris Conlee
wxWidgets Learner
CLion 2918.2.5 / CMake 3.12
wxWidgets 3.1.1
martin124
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Feb 21, 2020 7:23 pm

Re: Using wxWidgets with CLion -- SOME ANSWERS (2018)

Post by martin124 »

That was very usefull guide, thank you!

I would also add a trick to activate window when running app from clion on macos. I have a problem with that, window is always in the back of the clion - I am testing it on minimal sample app.

To activate main window (or rather frame) of the application we are running I added following code just after the:

frame->Show(true);

new std::thread([] {
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
system("osascript -e \"tell application \\\"minimal\\\" to activate\"");
});

it starts a new thread in which it first sleeps for 1s (allowing frame to fully show), then I execute shell script to activate app window. In this case application name is 'minimal'. This also requires including headers of chrono and thread, and lifting minimal c++ version to 14 or 17 ( set(CMAKE_CXX_STANDARD 17) in CMakeLists.txt which also requires lifting cmake version - I used 3.1.10)

Probably there is some other way to do this...
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using wxWidgets with CLion -- SOME ANSWERS (2018)

Post by ONEEYEMAN »

Hi,
It would be nice if all this can be dropped on wxWiki...

Thank tiy.
User avatar
conleec
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles
Contact:

Re: Using wxWidgets with CLion -- SOME ANSWERS (2018)

Post by conleec »

martin124 wrote: Fri Feb 21, 2020 7:29 pm That was very usefull guide, thank you!

I would also add a trick to activate window when running app from clion on macos. I have a problem with that, window is always in the back of the clion - I am testing it on minimal sample app.
Thanks for this. My development environment is dual monitor, so this never occurred to me. Also, regarding putting this info on Wiki, I have no idea how to do that. But if somebody truly finds it useful, and knows how to do that, I'd be happy to put it there.
---------------------------
Chris Conlee
wxWidgets Learner
CLion 2918.2.5 / CMake 3.12
wxWidgets 3.1.1
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using wxWidgets with CLion -- SOME ANSWERS (2020)

Post by ONEEYEMAN »

Hi,
You should get an account there and then just create a document, dropping this text there and save it.
It is as simple as writing an MS-Word file...

Thank you.
ehsun7b
In need of some credit
In need of some credit
Posts: 1
Joined: Thu Mar 26, 2020 11:11 am

Re: Using wxWidgets with CLion -- SOME ANSWERS (2020)

Post by ehsun7b »

You are my hero and my savior. :)

Awesome! Thanks a lot.
craigmac
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Feb 05, 2021 2:51 pm

Re: Using wxWidgets with CLion -- SOME ANSWERS (2020)

Post by craigmac »

Thanks for this! Helped point the way in finally getting the 'minimal' sample to build on 2020.3 CLion using CMake.

Here's what I'm using (Jan. 2021) and what I configured/built to build correctly:

* macOS Big Sur version 11.1
* 2020.3 CLion
* Source code downloaded from https://github.com/wxWidgets/wxWidgets/ ... .4.tar.bz2, and unzipped to ~/wx/3.1.4

I then did:

Code: Select all

cd ~/wx
mkdir build-debug-314 && cd build-debug-314
../3.1.4/configure --prefix="$(pwd)" --enable-debug --with-osx_cocoa --with-libpng --with-libjpeg --with-libtiff --with-cxx=17 --with-macosx-version-min=10.14
make
Without setting --with-macosx-version-min=10.14, it defaults to 10.10 and you'll get some WARNINGS about libraries compiled for 10.14 but using 10.10, so I bumped it to 10.14 to satisfy that. I also had to add --with-libjpeg or else
when you try to run the minimal project you will get a linker error about libjpeg, so I built all the wxWidgets
provided (tiff, png, jpeg libs at least) libraries rather than rely on the system (macOS) versions; it makes distributing the application easier and more portable as well.

Opened the ~/wx/3.1.4/samples/minimal folder with CLion same as OP did, and then run build. It works! At least the 'minimal' wxWidgets provided project built and ran. I'll update if I need to tweak or add/remove some details about 'minimal' project build, or any others that cause headaches for me.
MrStringkey
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Mar 03, 2023 3:11 pm

Re: Using wxWidgets with CLion -- SOME ANSWERS (2020)

Post by MrStringkey »

On OSX Monterey 12.6.2 I ran into a few issues with CLion 2022.3.2
The *minimal* example did not work as the find_packages has been updated

Code: Select all

if(APPLE AND NOT CMAKE_OSX_DEPLOYMENT_TARGET)
    # If no deployment target has been set default to the minimum supported
    # OS version (this has to be set before the first project() call)
    if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
        set(CMAKE_OSX_DEPLOYMENT_TARGET 12.0 CACHE STRING "iOS Deployment Target")
    else()
        set(CMAKE_OSX_DEPLOYMENT_TARGET 12.0 CACHE STRING "macOS Deployment Target")
    endif()
endif()

# Name the project
project(minimal)

# Request the required wxWidgets libs
find_package(wxWidgets 3.2 COMPONENTS core base REQUIRED)
did the trick both modifying the target version and the required package list.

After compiling from the command line I managed to build the project but it crashed when trying the run the debug build from CLion.
In the preferences of CLion I had to change the generator to 'XCode' for CMake, in the Build, Execution, Deploy settings. By default g++ was used and not Clang++.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Using wxWidgets with CLion -- SOME ANSWERS (2020)

Post by ONEEYEMAN »

Hi,
Yes gcc is kind of "not supported" on OSX. clang is.

Thank you.
Post Reply