0

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..

Static.Mike
  • 105
  • 6
  • If you have problem in **compiling wxWidgets** statically, why do you show **your code**? If you have problem in **using wxWidgets**, describe this problem (it could be an **error message**, or so). – Tsyvarev May 29 '18 at 21:00
  • @Tsyvarev, it compiles fine- everything works. It's just that whenever I try to copy the executable file (the application) to a different computer it just doesn't work or shows up as a text file with corrupted characters. Excuse me if I wasn't clear from the beginning. – Static.Mike May 29 '18 at 21:09
  • `shows up as a text file with corrupted characters.` - Do you attempt to run executable on Windows, while it was built for Mac? "just doesn't work" - Does it show some error message while run, or what? The problem is still unclear for me. – Tsyvarev May 29 '18 at 21:13
  • Ok, so... Clion compiles my program just fine. It creates an executable file of "Unix Executable" type. It's located in my project directory. When I click on it(on my mac), it invokes terminal and then right after the application itself comes on. I tried copying the executable to my brother's macbook and it did not work there. Launching it on a different mac works as if I were opening a .txt file. The executable file only has 198KB so I suppose something is not right. – Static.Mike May 29 '18 at 21:19
  • How are you transferring the file between the two computers? And you say it should "work on any computer"; this is not feasible (and probably completely impossible) if by "any computer" you also want it to work on Windows and Linux. In general executable files only work on the sort of system they are compiled for without some sort of emulation or translation. – Daniel H May 29 '18 at 21:25
  • @DanielH By any other computer I obviously meant any other mac computer for that matter. I have to send in the end program to my professor in a way in which he could just click on it and have it working without any hassle. The question is how do I do that(assuming he also has a mac)? I transfer the exec file by mail. – Static.Mike May 29 '18 at 21:27
  • @Static.Mike I would have guessed that, but if a lot of your classmates were using Windows I was confused. – Daniel H May 29 '18 at 21:28
  • @DanielH I really don't want to convert the whole program to be Windows compatible as there's a lot of directory path manipulation involved in the app and I can see a lot of problems with converting backslashes to slashes etc... :/ – Static.Mike May 29 '18 at 21:31
  • @Static.Mike, with wxWidgets you don't have to convert anything. But that beside the point. You should've asked the professor from the beginning whether (s)he has a Mac or the assignment is Windows only. Failing that, you should just do following: – Igor May 29 '18 at 21:35
  • @Static.Mike, `cd wxWidgets/buildMac && rm -rf * && ../configure --disable-shared && make`. Then switch to the folder where your source code reside and do this "g++ -o test.exe `~/wxWidgets/buildMac/wx-config -cxxflags --libs`". this should generate the appropriate application which will be copyable/transferrable to any Mac machine. Just adjust the path where appropriate. – Igor May 29 '18 at 21:38
  • @Static.Mike, CMake is still WIP for wxWidgets so you should be using the official supported way of doing things. – Igor May 29 '18 at 21:40
  • @Igor, thanks for your help! I wasn't able to follow your instructions yet, as I'm struggling to find the directory in which homebrew has installed wxWidgets. Is there a quick command to retrieve the directory under which the wxMac or wxWidgets resides? – Static.Mike May 29 '18 at 21:57
  • @Static.Mike, my suggestion - download the official source code from wxWidgets web site and compile yourself. On *nix-like OSes package managers installs everything dynamically linked. – Igor May 29 '18 at 22:17
  • @Igor I remember struggling to get it running this way, hence why I went for the brew solution. I will try redoing it statically tomorrow (it’s almost 1am according to my timezone) and report back. Thanks again for a lot of help! – Static.Mike May 29 '18 at 22:47
  • @Igor If you know how to build the wxWidgets static libraries and link against them, it might be nice to post a proper answer. There are already too many comments in this thread. – Paul Sanders May 30 '18 at 09:24
  • @Igor I have downloaded the Source wxWidgets library. I have created a build-release folder, ran ../configure --disable-shared and then executed make. executing g++ as you said above does not work I get: clang: error: unsupported option '--libs' and the same but for '-cxxflags'. – Static.Mike May 30 '18 at 11:32
  • I also tried the following: " g++ widgetTest.cpp `wx-config --disable-shared --cxxflags --libs` -o widgetTest " this would throw the following errors: main.cpp:3:10: fatal error: 'wx/wx.h' file not found #include – Static.Mike May 30 '18 at 12:11
  • @Static.Mike, can you build the minimal sample? just do `cd wxWidgets/buildMac/samples/minimal && make && open minimal.app`. Then you can try to just copy the build commands, change the source file names and compile/link your code as a workaround. – Igor May 30 '18 at 14:27
  • @Igor Except for the fact that the file hierarchy is build-release/samples/minimal them yes- it does work. – Static.Mike May 30 '18 at 17:15

2 Answers2

1

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.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • that is true. However it appears (s)he using homebrew which installs the dynamic libraries by default as any other package manager by default. So the real issue is to have a static link wxWidgets build and then rebuild the application as a bundle. – Igor May 29 '18 at 22:28
  • Well, OK, so there are two issues. Thought there might be. – Paul Sanders May 29 '18 at 22:34
1

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.

VZ.
  • 21,740
  • 3
  • 39
  • 42
  • Thank you for helping. What do you mean exactly by "configure it"? Should I configure the wxWidgets itself? or should I add these flags into cmake? I have found a wx-config file in /wxmac/bin folder. I guess that may be the actual config file? If that's the actual file I'm looking for where should I add these flags? – Static.Mike May 30 '18 at 11:12
  • These options should be used when running wxWidgets configure script, e.g. `$wx/configure --disable-shared --disable-sys-libs`. You must build wxWidgets yourself, if it had already been built referencing some libraries specific to your system, there is nothing you can do about these dependencies and you'd have to distribute them with your application (which is relatively simple under Mac as you can just put them into your bundle). – VZ. May 30 '18 at 20:33