3

When I try to run my C++ project in Code::Blocks IDE, it'll run the program in Windows CMD without any problem. If I try to run the program via Explorer it'll also run in CMD without any problem.

However, if I try to run the exactly SAME program via CLion, it'll run fine in CLion itself (CLion has a builtin terminal, while Code::Blocks uses Windows CMD). But if I try to run the program via Explorer I get this weird error:

error1 printscreen

If I press OK:

error1 printscreen

Anyone knows what the problem is? How do I fix this?

More information: Code::Blocks comes as a package with MinGW. With CLion I had to manually download a Toolchain. I downloaded and installed MinGW-w64, because I didn't know anything else and that's what Code::Blocks also used.

The weird part about this is that MinGW-w64 is installed in "Program Files (x86)" and not in "Program Files".

kenlukas
  • 3,616
  • 9
  • 25
  • 36
ilivss
  • 53
  • 6
  • 3
    It seems that the MinGW `bin` (or `lib`) directory is not in the `%PATH%`. – Some programmer dude Oct 19 '18 at 12:43
  • 3
    Your program isn't independent of the whole world. Your program requires the runtime library of your compiler to be visible to it to run. This is because you compiled your program with a shared-runtime setting. So, to fix this, either do what Some programmer dude said, and add the bin dir of gcc to the environment variable %PATH%, or compile in a static runtime setting. – The Quantum Physicist Oct 19 '18 at 12:47
  • @TheQuantumPhysicist I want the program to run on any computer without having to install MinGW. How do I "compile in a static runtime setting" in CLion? Sorry for asking I'm just a student. – ilivss Oct 19 '18 at 12:52
  • @ilivss You remind me of my very young days... like 18 years ago. I had the same problem with Visual C++ 6. Anyway, I think adding the flag `-static` to your compile command should do it. Read more about static linking with the runtime library in MinGW to learn more about this. – The Quantum Physicist Oct 19 '18 at 12:56
  • See https://stackoverflow.com/questions/24648357/compiling-a-static-executable-with-cmake – Some programmer dude Oct 19 '18 at 12:56
  • @Someprogrammerdude I just started this course 6 weeks ago. It is my first time programming so we share the same experience. Thank you for your information. It worked. I had to add `set(CMAKE_EXE_LINKER_FLAGS "-static")`. I'll definitely read more about this. – ilivss Oct 19 '18 at 13:08

1 Answers1

5

Add the following to your CMakeLists.txt file in CLion:

set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-static")

This will work for gcc, which mingw is based upon. For other linkers, you would have to search for their particular switch for static linking.

Gardener
  • 2,591
  • 1
  • 13
  • 22
  • I have a question: what is the difference between your code and `set(CMAKE_EXE_LINKER_FLAGS "-static")`? What does the extra `${CMAKE_EXE_LINKER_FLAGS}` do? Both lines work for me. – ilivss Oct 19 '18 at 13:11
  • All the comments under the question are accurate. If you also create libraries in your project, then you can add an additional line to statically link the libraries: set(BUILD_SHARED_LIBS OFF), as explained by @dude's reference to https://stackoverflow.com/questions/24648357/compiling-a-static-executable-with-cmake. – Gardener Oct 19 '18 at 13:12
  • 1
    The ${CMAKE_EXE_LINKER_FLAGS} is added to ensure that the new line does not modify the current linker flags. This concatenates the old linker flags with the new flag. – Gardener Oct 19 '18 at 13:13
  • Thank you for the explanation. I understand now. – ilivss Oct 19 '18 at 13:29