0

I am currently working on a project where on the start program reads the config file and perform some operations on it . But when i run the programme it works fine when i compile it via CLion or any other IDE but do not work when i run it via terminal by creating a separate build folder. when i debug i get to to know that when i run it via terminal the CMAKE_CURRENT_BINARY_DIR is build and when i run it via some IDE it is build/src. My exe file is generated inside src folder. So to make thing work i copied the Config file to both directories and it is working fine . But that is not a good approach i wanted to have a single config file for both environment . I am new to CMake any help would be really appreciated.

Here is my cmake file

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(test)

set(CMAKE_CXX_STANDARD 17)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.ini ${CMAKE_CURRENT_BINARY_DIR})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.ini ${CMAKE_CURRENT_BINARY_DIR}/src/config.ini)
add_subdirectory(src)

Umar Farooq
  • 418
  • 2
  • 14
  • 3
    How do you run `cmake` from the terminal? How do you attempt to run the program? How does it "not work" when you run from a terminal? Please take some time to refresh [ask] as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and [edit] your question to show exactly what you do and what happens and what's supposed to happen. – Some programmer dude May 20 '20 at 06:01
  • typical way of running it . >cmake .. >make >./src/netmain netmain is the name of my exe – Umar Farooq May 20 '20 at 06:02
  • You haven't provided enough information to understand what is going wrong. We can only *guess* at this point. You are missing some of your CMake code, as the `add_subdirectory()` command suggests you have more code you are not showing. Also, as commented earlier, please describe in your question post what exactly is *not working* and what the **desired behavior** is. – Kevin May 20 '20 at 12:51

1 Answers1

0

CLion probably sets the output automatically. For the command line try adding this to the MakeFile:

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

https://stackoverflow.com/a/6595001/7436984.

MeD
  • 13
  • 6
  • /usr/bin/ld: cannot open output file .: Is a directory collect2: error: ld returned 1 exit status src/tests/CMakeFiles/tests.dir/build.make:172: recipe for target 'src/tests' failed make[2]: *** [src/tests] Error 1 CMakeFiles/Makefile2:420: recipe for target 'src/tests/CMakeFiles/tests.dir/all' failed make[1]: *** [src/tests/CMakeFiles/tests.dir/all] Error 2 Makefile:129: recipe for target 'all' failed make: *** [all] Error 2 – Umar Farooq May 20 '20 at 07:09
  • i tried to but it showing this error . My CMake line is this set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/src) – Umar Farooq May 20 '20 at 07:09
  • The error message means that the place where resulted executable is needed to be created is **already used** for some directory. Probably, you need to choose other output directory. BTW, the error message is about `tests` executable but your question post doesn't describe it. – Tsyvarev May 20 '20 at 08:32