Author Topic: Setting #define for project (not in Source Code)  (Read 9488 times)

Offline duderino65

  • Single posting newcomer
  • *
  • Posts: 7
Setting #define for project (not in Source Code)
« on: May 02, 2017, 12:40:50 pm »
Hello,

I'm Andi from Germany. I currently developing for STM32 and recently switched from Windows to Linux and made up a new environment on my Linux Host.
I use an Keil ARMCC for Linux and a Makefile with 2 targets (CPU1 & CPU2). Building from Makefile works fine.
My little problem now is, that I define FIRMWARE_CPU1 or FIRMWARE_CPU2 in my Makefile depending on the target I'm building. But Codeblocks doesn't know that and does not see any FIRMWARE_xxx define in Source Code.

How can I setup a difine which is just valid for my project? As I'm developing on a device with 2 Mikrocontrollers I also have 2 Codeblocks Projects. So each of this projects need its own FIRMWARE_CPU define. Is this possible?

P.S.: I've experienced, that a configuration in Settings->Compiler->.... is alway for Codeblocks itself. Not just for the project... So this would be no solution, I think.

Thanks and kind regards
Andi

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1564
Re: Setting #define for project (not in Source Code)
« Reply #1 on: May 02, 2017, 02:07:01 pm »
Try under Project -> Build options,  inside tab Compiler settings select tab #defines

Offline duderino65

  • Single posting newcomer
  • *
  • Posts: 7
Re: Setting #define for project (not in Source Code)
« Reply #2 on: May 02, 2017, 03:16:37 pm »
Hi,
thanks for reply. Unfortunately I don't have a #define-Menu in my Build Options. This #define Menu I only have in Settings->Compiler. But as already said, this Menu is for Codeblocks in general, not only for a particular project.
I attached my Build Options Menu as I see it and my actual Codeblocks About Menu.

Can I define it in these Variables Tab?

Kind regards
Andi

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7592
    • My Best Post
Re: Setting #define for project (not in Source Code)
« Reply #3 on: May 02, 2017, 03:20:50 pm »
PLEASE always state when you are using an CB Custom makefile project instead of a normal CB Project!

Edit: The options are missing because they are ignored by an CB Custom makefile project.

You need to read up on how makefiles work and then look at the makefile.

You will likely need to edit the makefile; but, the best answer make be to pass an option to the makefile.

Tim S.
« Last Edit: May 02, 2017, 04:05:13 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline duderino65

  • Single posting newcomer
  • *
  • Posts: 7
Re: Setting #define for project (not in Source Code)
« Reply #4 on: May 02, 2017, 03:39:23 pm »
Hi,

sorry, I mentioned that I use a Makefile and with that I also checked the 'This is a custom Makefile' in my Projects Options. So when I hit the Build Icon my Makefile is running.
Even if I don't have much experience with Makefiles, I created this Makefile by my own and I know how it normally works. As I use this Makefile also for CI Builds I don't want to change it and I don't see a need to change it.

From my point of view it is just a markup problem from the IDE because it does not see this Define (not a Bug!). So my idea was, if possible, just to do a #define somewhere in the project settings. I want my Makefile to be completely independent from any IDEs.

I hope this is somehow possible.

Kind regards
Andi

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7592
    • My Best Post
Re: Setting #define for project (not in Source Code)
« Reply #5 on: May 02, 2017, 04:06:49 pm »
I will try to find my custom makefile project solution; note: It may or may not work for you.

Edit: Link to Wiki page that might help you http://wiki.codeblocks.org/index.php/Code::Blocks_and_Makefiles

It been several months to possibly years since; I used my custom makefile project solution, it might no longer work.

But, you should be still able to add options to the make command to solve your problem with defines.

Tim S.
« Last Edit: May 02, 2017, 04:42:17 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7592
    • My Best Post
Re: Setting #define for project (not in Source Code)
« Reply #6 on: May 02, 2017, 06:11:06 pm »
Edit: See "Make" Commands tab.

I added CPPFLAGS="-DWINVER=0x0601" to define WINVER with the value 0x0601.

This will NOT work with all makefiles; but, should work with most well written ones.

Tim S.

The commands below are from a CB Project I created to build wxWidgets.

Code
$make -f $makefile MONOLITHIC=0 SHARED=1 BUILD=debug CPPFLAGS="-DWINVER=0x0601"

Code
$make -f $makefile MONOLITHIC=0 SHARED=1 BUILD=debug CPPFLAGS="-DWINVER=0x0601" $file
« Last Edit: May 02, 2017, 06:13:47 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Setting #define for project (not in Source Code)
« Reply #7 on: May 02, 2017, 09:49:36 pm »
@duderino65:
Please describe your problem with more details, the current description just causes confusion!
Where does C::B can't see your derine?
It seems it is not related to building, but something in the editor.
Is your problem that C::B's editor greys out some parts of the code?
Or is it that code completion doesn't see some functions/variable/marco guarded by defines?
(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 sodev

  • Regular
  • ***
  • Posts: 497
Re: Setting #define for project (not in Source Code)
« Reply #8 on: May 03, 2017, 03:12:13 am »
This problem sounds quite familiar to me, im pretty sure his problem is code completion. For code completion to work it needs to know the current compiler configuration. He is using an external Makefile and this configures the compiler, in his case it especially sets certain defines. Apparently codeblocks doesnt read the makefile and therefore code completion runs with a different compiler configuration, especially it doesnt know these defines and code completion will evaluate all #ifdefs querying these defines to false and e.g. show the code parts as disabled.

The problem is actually more complicated, because defines are not the only information code completion needs, e.g. it makes a huge difference if gcc runs in c++ 11 mode or not. I dont know how code completion in CodeBlocks solves this, i have the same problem in Eclipse when using an external Makefile. But in Eclipse, even when using an external Makefile, you can still set compiler defines, include paths and compiler command line parameters so that its code completion can query the compiler internal defines and other settings. But in CodeBlocks these settings are all missing for a custom Makefile project, so he cant adjust the compiler configuration that code completion sees to the one that his makefile actually uses to compile the code.

In his case adjusting the global compiler configuration doesnt work because his project uses different settings per target, he needs the target specific settings to be available.

Offline duderino65

  • Single posting newcomer
  • *
  • Posts: 7
Re: Setting #define for project (not in Source Code)
« Reply #9 on: May 03, 2017, 07:52:00 am »
Hi all,

@Tim S.: Thanks for your big effort. I'm really sorry, I didn't describe my problem very well.

As "sodev" already wrote, my problem is that CB disables some code parts which normally are active for this particular target. So it is harder to get / see these code parts which is somehow painful at programming. I thought it would be possible somehow to add such a define for the particular project. If this is not possible, it is not a show stopper. Maybe I can adjust the highlighting of the editor.

Thanks for your help.

Kind regards
Andi

Offline duderino65

  • Single posting newcomer
  • *
  • Posts: 7
Re: Setting #define for project (not in Source Code)
« Reply #10 on: May 03, 2017, 08:18:21 am »
Hi again,

I solved this for me with some changes in highlighting of the editor.
In the Compiler Settings I, anyway, added a define for FIRMWARE_CPU1. In the Settings of the editor I set the "Default (Inactive)" color to a darker gray. So my active code is black and the inactive is dark grey.
As I normally program in one project for both targets this solution is ok for me. I can see the deactivated code much clearer.

Kind regards
Andi

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Setting #define for project (not in Source Code)
« Reply #11 on: May 03, 2017, 08:38:51 am »
Just disable this darkening and live a happy life. :)
(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!]