Author Topic: Adding files to a project created with cmake  (Read 434 times)

Offline gib

  • Multiple posting newcomer
  • *
  • Posts: 32
Adding files to a project created with cmake
« on: July 04, 2017, 11:47:19 pm »

I think I know the answer to this question, but I want to be sure, and possibly get an explanation.
I am working with a project (C + Fortran) that was created from CmakeLists.txt like this:

cmake -G "CodeBlocks - MinGW Makefiles"

I thought I could add source code files to the project via 'Add files...', but when I build the project the added code is not found and the link fails.  It seems that CB is using the previously created makefiles (the 'Custom makefile' box is ticked in Properties) and ignoring the displayed list of files.  If I untick the Custom makefile box I get build errors, whether I choose GNU GCC or GNU Fortran as the compiler.  There is always the option of adding the files in CmakeLists.txt - that works.

I know that when I create a project within CB, without using cmake, I can add files any time.  With a cmake-generated project, is it always necessary to edit CmakeLists.txt and rerun cmake?  If it was just a matter of adding the source files it would be a non-issue, but when many libraries and search paths have been added within CB the changes needed in the cmake file become much more complicated, in fact I'm not sure how to make them.  Adding them again within CB after rerunning cmake can be rather tedious.

I'm really trying to get a clearer idea of what happens 'under the hood' of CB.

Thanks
Gib
Modify message

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7200
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: Adding files to a project created with cmake
« Reply #1 on: July 04, 2017, 11:57:56 pm »
You should ask this in a cmake-related list.

Using custom makefile-projects (like cmake does) is not the preferred way to use Code::Blocks.

I'm really trying to get a clearer idea of what happens 'under the hood' of CB.
Code::Blocks just runs the makefiles and files, search paths or libs added to the project in C::B are (of course) not known by the makefile.

Offline gib

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Adding files to a project created with cmake
« Reply #2 on: July 05, 2017, 12:20:38 am »
Thanks jens, that confirms what I suspected.  I will explore creating this project within CB.

Offline gib

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Adding files to a project created with cmake
« Reply #3 on: July 05, 2017, 01:48:07 am »
I now have a working project that was developed within CB.  This is definitely preferable to what I was doing with cmake.

One comment: in a project like this with mixed languages, C and Fortran, when I set the Project Build options for the C compiler, it automatically assumes that this has become the Selected compiler, requiring me to reset it back to Fortran.  Is there a way to change the options for one of the compilers without changing the compiler selected for the project?  (Fortran needs to be the selected compiler in this project.)  It's a minor issue.

Offline gib

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Adding files to a project created with cmake
« Reply #4 on: July 05, 2017, 03:42:43 am »
Hi jens, I've noticed that the .cbp file sometimes gets created with two <Linker> sections, one contained within the <Build> section, the other outside and after it.  I presume this is because the project uses two compilers, and maybe the <Build> section refers to C.  If these two <Linker> sections are not consistent build errors can result.  I have found that it is safe to delete the first <Linker> section (the one inside <Build>).  When the Project Build options are changed in CB, the change in the project file occurs only in the <Linker> section outside the <Build> section, so after that first section is deleted it doesn't get replaced (so far!).

The moral of the story is that I have to remember to check what's in the project file.  By the way I'm still using version 13.12 - maybe the latest version has different behaviour.

Cheers, Gib

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7200
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: Adding files to a project created with cmake
« Reply #5 on: July 05, 2017, 05:55:52 am »
In the projects build options you can chose per target, whether to prepend or append target options to the project options or to use just project or target options (see the policy dropdown-box).

Offline gib

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Adding files to a project created with cmake
« Reply #6 on: July 05, 2017, 06:41:23 am »
I never looked at the Policy - that clarifies things a lot.

Thanks!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 10194
Re: Adding files to a project created with cmake
« Reply #7 on: July 05, 2017, 10:01:53 am »
I now have a working project that was developed within CB.  This is definitely preferable to what I was doing with cmake.
I'm not sure any more that our build system is preferable to cmake's.
To add file to the cmake's build you just edit the cmakelist.txt file that is responsible for the project/target you want to add the file to, then run "cmake ." or build inside codeblocks and the file will be added correctly.
Yes, there is no gui, but using cmake allows you to do way more complex build configuration that will ever be possible in codeblocks.
Also if you have many targets/projects and you're using make or ninja the builds will be faster.

And lastly it seems that C/C++ community is converging to using cmake as the tool to setup/control builds. I don't know if this is good or bad, be we need to support it well in codeblocks...
<debugger plugin maintainer>
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline gib

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Adding files to a project created with cmake
« Reply #8 on: July 05, 2017, 11:31:51 am »
There is no doubt that cmake is becoming (or has become) the standard build tool.  It is extremely powerful, and as you say allows much more complex builds than CB.  But for people like me who are never going to develop cmake expertise the simpler, more limited, GUI-based approach of CB makes it easier than writing a CmakeLists.txt file.  I often use cmake, but I'm using CmakeList files developed by somebody else.  The comparison between cmake and CB illustrates the inverse relationship between software power and user-friendliness.