0

So, I am starting out with CLion. And I have a project where I managed to store the .cpp and .h files in a folder under the working directory called src. However, I am unable to get the linker's generated output (the executable file) in a separate folder, let's say bin under the working directory.

For reference, the file structure of the project that I am seeking is:

  • Working Directory
    • make-build-debug
    • CMakeLists.txt
    • src
      • example.cpp
      • example.h
    • bin
      • example (example.out)

Platform Specification:

  1. OS: macOS 13.4.1
  2. CLion Version: 2023.1.4

CMakeLists.txt:

cmake_minimum_required(VERSION 3.25)
project(Password_Manager_CPP)

set(CMAKE_C_STANDARD 11)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

include_directories(.)

add_executable(Password_Manager_CPP
        src/PasswordManager.cpp src/PasswordDriver.cpp src/PasswordManager.h)

For example, if you have a file example.cpp, and you compile it using the command: g++ example.cpp -o example. I want the UNIX Exec file, example, to be stored in the bin folder.

I tried out some potential solutions on the internet. I looked into YouTube, Google, forums, and even StackOverflow. However, I could not find a solution that sufficed my needs.

  • "the compiler's generated output (the executable file) " - isn't that the linker's output? AFAIK, the compiler creates OBJ files and the linker creates the executable. – Thomas Weller Jul 13 '23 at 18:00
  • Setting `CMAKE_RUNTIME_OUTPUT_DIRECTORY` should work, unless CLion (or macos) somehow screws around with cmake's normal behaviour. If you're using a multi-config generator the binaries end up in a configuration-specific subdir (e.g. `bin/Debug`), but there are workarounds for that... If it's indeed CLion using a modified cmake version, you may be able to get the binaries to the desired location using `install()` commands + the cmake install step. – fabian Jul 13 '23 at 18:22
  • Btw: I strongly recommend not putting any build results into the source tree by default, since makes use of the source dir with multiple cmake configurations hard, if not impossible. – fabian Jul 13 '23 at 18:22
  • Does this answer your question? [How do I make CMake output into a 'bin' dir?](https://stackoverflow.com/questions/6594796/how-do-i-make-cmake-output-into-a-bin-dir) – starball Jul 14 '23 at 01:24
  • @ThomasWeller You are correct. My terminology wasn't right. – Suswagatam Rong Jul 14 '23 at 07:53
  • @fabian Hey, I am not looking for the build results specifically. Imagine you have `example.cpp`, and you run the command `g++ example.cpp -o example`. So, this command will generate an executable file called `example`. I want this file to be in the `bin` folder instead of the main working directory, as CLion is doing. – Suswagatam Rong Jul 14 '23 at 07:56
  • @starball I actually have looked into that post. And I tried out the solutions. However, none worked. – Suswagatam Rong Jul 14 '23 at 07:57
  • you're supposed to say that in your question post and give details about what you observed when you tried them rather than "it didn't work". Even better if you show exactly _how_ you tried them. Maybe you just did it wrong. – starball Jul 14 '23 at 08:12
  • @starball I am learning more about using this platform. Thank you for your input. As I can see, quotations did the job for me. Thank you for putting the effort into helping me out. I appreciate it. – Suswagatam Rong Jul 15 '23 at 04:13

1 Answers1

0

Okay, after quite a bit of surfing through different documentation, and forums. I found a way that works for me.

CMakeLists.txt

set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/bin")

Using this in your CMakeLists.txt puts the linker's output into your desired folder.

Helpful resources:

You can use the below-written code too.

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")

One thing to notice here, the path is within a quotation, which I was not doing before.

I would advise the latter one, as within my understanding of CMake as of now, the variables used in the latter block of code supersedes the previous.