Using wxWidgets with CLion -- SOME ANSWERS (2018)

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
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles
Contact:

Using wxWidgets with CLion -- SOME ANSWERS (2018)

Post by conleec » Tue Oct 16, 2018 12:12 am

Hello everybody,

First of all, let me start off by saying that I am a complete and utter newbie. I've spent weeks, literally, trying to figure out how to build this library, and how to build and run the samples using my IDE of choice, CLion. I'm Mac based, currently, but I suspect what I've learned is transferable to Windows as well...

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.

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 » Mon Apr 15, 2019 9:20 pm

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
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Aug 22, 2018 4:19 pm
Location: Los Angeles
Contact:

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

Post by conleec » Mon Apr 15, 2019 10:26 pm

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

Post Reply