Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: troels on January 18, 2006, 01:08:16 pm

Title: Moving to GCC question
Post by: troels on January 18, 2006, 01:08:16 pm
Hi all,
I'm moving a MSVC6 project (an entire workspace really) to C::B. Import went well.

1. GCC and relative #include paths:

This compiles: #include "../include/someheader.h"
This doesn't compile: #include "../../include/someotherheader.h" ("No such file or directory")

Apparantly (this version of) GCC cannot handle two updirs ("../..").
Any way around this limitation?

Regards
Troels

Using 1.0rc2 with MinGW compiler, updated with Nightly Build Jan. 16
Running on Win2000 SP4, WinXP SP2
Title: Re: Moving to GCC question
Post by: mandrav on January 18, 2006, 01:15:38 pm
GCC has no problems with two updirs (or three or four or whatever).
Just realize that it needs to find the included file...
Title: Re: Moving to GCC question
Post by: thomas on January 18, 2006, 01:17:16 pm
More likely, your include paths are set up incorrectly. gcc (or MinGW for that matter) normally handles many levels of ../../../ without any problems.

Check that whatever it is you try to include really exists where the compiler would expect it (i.e. from one of the paths supplied as include paths).
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 01:43:03 pm
Check that whatever it is you try to include really exists...

It certainly does. It compiles in MSVC.

GCC has no problems with two updirs (or three or four or whatever).

Good to hear, quite a relief.

This might be a C::B issue then:

- If I right click the offending .c file and choose "Build file" in the menu it compiles!
- If I press Ctrl+F9 it doesn't compile ("No such file or directory")

Here's a little test C::B project for demonstrating this:
http://www.trak.dk/gcctest.zip
Also included in the zip is the corresponding MSVC project.
It compiles in MSVC, not in C::B (using Ctrl+F9)

Thanks
Troels
Title: Re: Moving to GCC question
Post by: Michael on January 18, 2006, 02:14:07 pm
After adding the include directories, I had to modify the #include (mylib\src\mylib.c) in:

Code
#include "mylib.h"

Then it compiled.

Quote
-------------- Build: Win32 Debug in gcctest ---------------
Compiling: .\mylib\src\mylib.c
Linking console executable: C:\test\gcctest\Debug\gcctest.exe
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

Michael
Title: Re: Moving to GCC question
Post by: thomas on January 18, 2006, 02:21:17 pm
It certainly does. It compiles in MSVC.
It is not as certain as you think.

In the project which you supplied, for example, none of the headers exist from the point of the compiler's view, there are no include directory settings at all, and the include files are in a subfolder.
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 02:26:15 pm
After adding the include directories, I had to modify...

Sorry, buy this (gcctest.zip) is a perfectly valid project as is, I believe.
The idea is to not change anything. Especially not the source files.

In the project which you supplied...there are no include directory settings at all

This is on purpose.

But, perhaps moving to gcc just requires adding include directories (adding gazillions of include directories if you have gazillions of subprojects) ??

Thanks
Troels
Title: Re: Moving to GCC question
Post by: thomas on January 18, 2006, 02:45:07 pm
This is on purpose.

But, perhaps moving to gcc just requires adding include directories (adding gazillions of include directories if you have gazillions of subprojects) ??
Well, on purpose or not, it's wrong ;)

I don't know whether the MS compiler automatically looks into ../include or whether Visual Studio adds that path secretly, but it is a certain thing that the compiler cannot possibly find those files if it does not know where to look.
You don't need to add millions of paths for the same number of projects, since you can use relative pathnames, and if you don't like adding them to the individual projects, you can do so in global compiler options.
Title: Re: Moving to GCC question
Post by: Michael on January 18, 2006, 02:49:08 pm
After adding the include directories, I had to modify...

Sorry, buy this (gcctest.zip) is a perfectly valid project as is, I believe.
The idea is to not change anything. Especially not the source files.

Sorry, but I think it is not valid. If I add the include directories to C::B, then I have to modify as above, if I add no include directories, then I have to modify the #include in:

Code
#include "./mylib/include/mylib.h"

The problem is when you give a relative path: from where begin the search?

Michael
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 02:56:31 pm
Code
#include "./mylib/include/mylib.h"
The problem is when you give a relative path: from where begin the search?

I believe it's fairly clear, apostrophes ("") means 'here' (the location of the .c file with the #include in it), lessthan-greaterthan (<>) means search include paths (first). But I could be wrong I guess.

This is on purpose.
Well, on purpose or not, it's wrong ;)

It can't be entirely wrong, rightclicking -> "Build File" works.

/Troels
Title: Re: Moving to GCC question
Post by: Michael on January 18, 2006, 03:01:19 pm
Code
#include "./mylib/include/mylib.h"
The problem is when you give a relative path: from where begin the search?

I believe it's fairly clear, apostrophes ("") means 'here' (the location of the .c file with the #include in it), lessthan-greaterthan (<>) means search include paths (first). But I could be wrong I guess.

Well, may be with VC++ 6. It works fine there. But with C::B not. May be a C::B dev can give some further info or instead confirms that I am wrong :).

Michael
 
Title: Re: Moving to GCC question
Post by: thomas on January 18, 2006, 04:00:45 pm
http://www.cppreference.com/preprocessor/include.html

EDIT:
The exact method supported by gcc is specified by C99, as described here: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf on page 149
Title: Re: Moving to GCC question
Post by: TDragon on January 18, 2006, 04:04:49 pm
The problem is when you give a relative path: from where begin the search?
Spot on.

From what I've seen, the critical difference lies between specifying a qualified path (one with slashes) and a plain filename. For an include directive that only specifies a file name, GCC looks in the directory it was invoked from, the directory the file being compiled is in, and any additional directories specified with the -I option. For an include directive that specifies an absolute or relative qualified path, apparently it looks in the directory it was invoked from and any additional directories from the -I option, but NOT the directory the file being compiled is in. Code::Blocks calls GCC from the directory your .cbp file is in, so this is probably where your compilation is going wrong.
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 04:21:10 pm
...apparently it looks in the directory it was invoked from...but NOT the directory the file being compiled is in

Excellent! Thanks TDragon!

I've posted a bug report, #1409127.

Regards
Troels
Title: Re: Moving to GCC question
Post by: TDragon on January 18, 2006, 04:36:34 pm
Now that I've posted all that, I'm not so sure anymore; look at the directory structure for one of my projects:
Code
Cklo
 |- Source
     |- Engine
     |   |- Engine.cpp {#include "../base.h"}
     |- base.h

Engine.cpp compiles without adding the "Engine" directory to the include paths. So my answer before appears to be wrong.
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 04:41:39 pm
Now that I've posted all that, I'm not so sure anymore

I'm quite sure. My projects also compiles one updir (#include "..") for some reason, it breaks only on two ("../../").
(Again, rightclick -> "Build File" works, Ctrl+F9 doesn't)

Excellent! Thanks TDragon!
And thanks to Michael,Thomas and Mandrav too!

/Troels
Title: Re: Moving to GCC question
Post by: TDragon on January 18, 2006, 04:47:27 pm
Ah, now that's an interesting gotcha. Probably deserving of a bug report to GCC if someone can get rigorous on it. For now, you would probably need to add all your source subdirectories under Code::Blocks' Project->Build Options->Directories->Compiler, to avoid making changes to the actual source files.
Title: Re: Moving to GCC question
Post by: tiwag on January 18, 2006, 04:53:12 pm
Here's a little test C::B project for demonstrating this:
http://www.trak.dk/gcctest.zip

add a Compiler directory "./mylib/src"

because all your relative pathes are relative to that dir

then it works

[attachment deleted by admin]
Title: Re: Moving to GCC question
Post by: Michael on January 18, 2006, 04:56:45 pm
add a Compiler directory "./mylib/src"

because all your relative pathes are relative to that dir

then it works

Interesting :).

Michael
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 05:02:46 pm
For now, you would probably need to add all your source subdirectories...

Been down that route. Not really feasible in largish projects. I can afford to wait - a while yet :)

/Troels
Title: Re: Moving to GCC question
Post by: tiwag on January 18, 2006, 05:02:55 pm
Interesting :).

it is because gcc is executed from the project directory (where the *.cbp resides) and all pathes are relative to this directory.

so

"./mylib/src"  + "../include/mylib.h"

and

"./mylib/src"  + "../../mylib/include/mylib.h"

are ok then
Title: Re: Moving to GCC question
Post by: TDragon on January 18, 2006, 05:12:23 pm
I can afford to wait - a while yet :)
You'd have to; even if the bug is fixed in the current version of GCC, it'll probably still be months before the MinGW team gets to it. I've never personally seen problems with search paths in the larger GCC projects I've worked on (witness Code::Blocks :)).
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 05:17:54 pm
I can afford to wait - a while yet :)
You'd have to; even if the bug is fixed in the current version of GCC...

Oh, but I have a feeling that this is a C::B bug (rightclick -> "Build File" works), not a GCC bug.
And they work fast here on this project :)
(I can't believe that you've already moved to wxAUI, that was bloody fast)

Regards
Troels
Title: Re: Moving to GCC question
Post by: Michael on January 18, 2006, 05:19:40 pm
Interesting :).

it is because gcc is executed from the project directory (where the *.cbp resides) and all pathes are relative to this directory.

Yes, I have remarked it. My solution would have been to add the 2 include directories in Compiler (relative paths) and then modifiy/add the #include opportunely. Your solution is shorter and avoid to add all the include directories. Thank you for the explanation.

Michael
Title: Re: Moving to GCC question
Post by: Michael on January 18, 2006, 05:24:06 pm
I can afford to wait - a while yet :)
You'd have to; even if the bug is fixed in the current version of GCC...

(rightclick -> "Build File" works)

In this case is not the compiler invoked from the directory where the source file is? And, if I remember correctly, the include paths are then correct.

Michael
Title: Re: Moving to GCC question
Post by: tiwag on January 18, 2006, 05:24:13 pm
Oh, but I have a feeling that this is a C::B bug (rightclick -> "Build File" works), not a GCC bug.

1. it is not a CB bug, CB is working 100% properly - see your project definitions
2. rightclick works, because gcc is executed in the directory where the source file resides also the *.o file is produced there.
3. add the compiler include directory(ies) properly and everything is fine.
Title: Re: Moving to GCC question
Post by: troels on January 18, 2006, 05:29:07 pm
it is not a CB bug...rightclick works, because gcc is executed in the directory where the source file resides...

I realized that a while back:

http://sourceforge.net/tracker/index.php?func=detail&aid=1409127&group_id=126998&atid=707416

/Troels
Title: Re: Moving to GCC question
Post by: troels on January 19, 2006, 02:22:38 pm
Thomas just closed (threw out) my 'bug' report:
https://sourceforge.net/tracker/?func=detail&atid=707416&aid=1409127&group_id=126998

"...IDE built specifically to meet the most demanding needs of its users"

Guess I'm too demanding then. I need my IDE/compiler to be fairly compatible with other compilers (MS).
C::B + MinGW comes so close.

Here's my 'bug' report again for posterity:

"
#include with quotes doesn't work for relative paths

#include "../../foo.h" does not compile in C::B.

In many cases this effectively prevents a clean compile
after importing a MSVC project/workspace, see
TDragons's (first) reply:
http://forums.codeblocks.org/index.php?topic=2039

I propose compiling in the source file directory (where
the .c/cpp file is located), rather than in the .cbp
directory.

If this breaks (too) many things, please consider this
a feature request:

A compile option (checkbox) such as "Compile this file
inplace (where the .c/cpp file is located)" would be
useful."

Regards
Troels

EDIT:
Notes:
- Adding include paths to everything and the kitchen sink is impractical, makes for (even) slower compilations, makes moving to C::B more of a hassle than necessary
- This particular problem might very well be considered a bug in/shortcoming of gcc, but still, I believe that the job of the IDE is to shield the user from (most) antics of compiler X, to take the drudgery out of software development.
Title: Re: Moving to GCC question
Post by: mandrav on January 19, 2006, 03:24:53 pm
Quote
"...IDE built specifically to meet the most demanding needs of its users"

Guess I'm too demanding then. I need my IDE/compiler to be fairly compatible with other compilers (MS).
C::B + MinGW comes so close.

The sentence you quoted from the website, does not mean that we 'll work for any crazy idea anyone might have. And for free. But you can always add/fix it yourself.

It's really sad when people show no respect at other people's hard work (free as in beer and in speech)...
Really, there is no reason to react like that...

EDIT:
Although I refuse to consider this as a bug but rather lack of configuration from the user's part, the fix has been committed.
Title: Re: Moving to GCC question
Post by: troels on January 19, 2006, 03:41:13 pm
Really, there is no reason to react like that...

Sorry then.

...you can always add/fix it yourself.

It would have to be acknowledged as a good idea first. Thomas simply discarded the 'bug' report, which could lead one to believe that the idea was unpopular with the devs here (wait for gcc itself to be fixed), hence unwanted. Not everybody have a long history with gcc, and gcc frontends, and knows exactly what to expect from it and not.

...the fix has been committed.

Quite a surprise. Amazing!

/Troels
Title: Re: Moving to GCC question
Post by: thomas on January 19, 2006, 03:47:43 pm
Thomas just closed (threw out) my 'bug' report:
https://sourceforge.net/tracker/?func=detail&atid=707416&aid=1409127&group_id=126998

"...IDE built specifically to meet the most demanding needs of its users"

Guess I'm too demanding then. I need my IDE/compiler to be fairly compatible with other compilers (MS).
You see, several people already told you. It is not a bug, neither in the IDE, nor in the compiler. You are not using the tools properly.

A compiler is not a divination apparatus, and neither is an IDE. If you put your files into some folder, then the compiler won't know unless you tell it.
Maybe Visual Studio does that differently (maybe they have the search path ../include built-in, I don't know), but this is not standard, and it is not correct.

The reason why the compiler does not scan large portions of your hard disk and use whatever files it can find is that you may have many different headers with the same name (for example there is a file string.h in the wxWidgets headers and a file with the same name in the system headers. You might even have a file with that same name too, although that would be bad design).
If the compiler just looked in arbitrary places and use whatever it happens to find, you would have a terrible mess every time.

The correct way of operation for a compiler is to have a clearly defined set of search paths (and a clearly defined order), so there is no question about which one to use.

Consequently, the correct way of operation for you is to tell the compiler what you want. If you don't do that, the compiler will not work properly.
Title: Re: Moving to GCC question
Post by: thomas on January 19, 2006, 03:54:07 pm
Although I refuse to consider this as a bug but rather lack of configuration from the user's part, the fix has been committed.
Now we do have a bug...  :?
Title: Re: Moving to GCC question
Post by: tiwag on January 19, 2006, 05:29:21 pm
@thomas : why do you think it's a bug, when CB adds the module's path to the compiler's include directories by default ???
Title: Re: Moving to GCC question
Post by: thomas on January 19, 2006, 05:52:59 pm
Because it is not right to add a path secretly. Although it will probably (hopefully) never cause a problem in this particular case, it still is not right. You don't know where it is inserted (first, last...?) either. Well, you would know if you looked at the sources, but the point is you don't even know that it is happening.

If we start adding paths, then why don't we add ./include, ../include, and ../../include too? We might add ../src/include and ../source/include as well, then the people who put their project files in some subfolder would be happy.

Following the same reasoning, we could secretly add -lkernel32 to the linker options under Windows because practically all Windows applications need that anyway. Does really anybody think this would be a good idea?

Even if this include path likely causes no problem, is that what we want, is that the way it should be? I say no. The compiler should do exactly what you tell it to do, not perform any MS-style capers.
Title: Re: Moving to GCC question
Post by: tiwag on January 19, 2006, 05:58:17 pm
hehe  :shock:
Title: Re: Moving to GCC question
Post by: klight on January 19, 2006, 06:11:36 pm
Forgive my ignorance, but perhaps there is a different approach...

Why couldn't the MS project/workspace import do the work and add the required paths to the job file?  While I realize this may be more work and may raise other issues, it would seem to be a less offensive approach to the core C::B design.
Title: Re: Moving to GCC question
Post by: rickg22 on January 19, 2006, 06:16:04 pm

Hmmm interesting idea. It may work. Ok, let me jump into the bandwagon and suggest the addition of this flag in the project's build options.

Quote
[ ] Add Extra search paths like MS-VisualStudio (WARNING: NOT recommended for cross-platform projects since it's not ISO-C++ compliant)

This flag could be set for importing MSVC projects :)
Title: Re: Moving to GCC question
Post by: thomas on January 19, 2006, 06:21:37 pm
Forgive my ignorance, but perhaps there is a different approach...

Why couldn't the MS project/workspace import do the work and add the required paths to the job file?  While I realize this may be more work and may raise other issues, it would seem to be a less offensive approach to the core C::B design.
That's not ignorance at all. That's the correct way :)
Title: Re: Moving to GCC question
Post by: mandrav on January 19, 2006, 06:35:20 pm
Because it is not right to add a path secretly. Although it will probably (hopefully) never cause a problem in this particular case, it still is not right. You don't know where it is inserted (first, last...?) either. Well, you would know if you looked at the sources, but the point is you don't even know that it is happening.

Well, I would normally agree with you but this is a case where it doesn't hurt. The reason is that what I added is "." (current dir) from the compiling-file's point of view. The current dir, unless overriden, is implicitely included in the search list. The only difference here is that because C::B uses the project's dir as the current dir, we "help" the compiler by adding the file's current dir in the list :).

I hope it makes sense the way I wrote it  :P
Title: Re: Moving to GCC question
Post by: tiwag on January 19, 2006, 06:40:05 pm
@thomas : you are right, IT IS A BUG !!!

after updating CB to rev 1813 i tried to rebuild any of my plugin's and earned a lot of undefined symbols errors

after reverting /trunk/src/plugins/compilergcc/cmdlinegenerator.cpp to rev 1810 it behaves ok again !
Title: Re: Moving to GCC question
Post by: killerbot on January 19, 2006, 10:03:58 pm
I agree with Thomas on this. We should not add secretely extra paths, to get something to compile.
Title: Re: Moving to GCC question
Post by: troels on January 19, 2006, 11:25:56 pm
The correct way of operation for a compiler is to have a clearly defined set of search paths (and a clearly defined order), so there is no question about which one to use.

Problem is that the number of search paths tends to add up in non-trivial projects, yielding a compilation speed hit. And gcc is a terrible slow compiler already (it has advantages too naturally, but not speed).
Likewise Yiannis' fix, neat and straightforward as it is, is bound to yield a slight compilation speed hit too (adding one more search path). I'd personally vote for invoking gcc in the source module's directory (no added search path), but perhaps Rick's suggestion is most likely to make everybody happy.

The compiler should...not perform any MS-style capers.

What's wrong in catering for MS-users a bit? Lots of potential users there. An elitist gcc-is-god stance is not likely to benefit the project much.

The sentence you quoted from the website, does not mean that we 'll work for any crazy idea anyone might have. And for free.

Likewise I provided feedback for free. Qualified feedback even, I believe. One dev makes a move to bury this suggestion of mine, and for objecting to this I get called crazy. I'll be silent.

Logged out.
Title: Re: Moving to GCC question
Post by: sethjackson on January 19, 2006, 11:44:34 pm
I agree with Thomas on this. We should not add secretely extra paths, to get something to compile.

Yup. I'll have to agree.
Title: Re: Moving to GCC question
Post by: Michael on January 20, 2006, 09:43:55 am
I agree with Thomas on this. We should not add secretely extra paths, to get something to compile.

Yes, I agree on this too.

Michael
Title: Re: Moving to GCC question
Post by: mandrav on January 20, 2006, 09:52:12 am
OK, I think enough have been said about the issue.
The commit log of r1820:

* Added compiler-independent option to explicitely add the currently compiling file's directory to the compiler's search dirs. This fixes once and for all the bug with revision 1813 and allows it to be configurable. Default is *not* to add this dir behind the scenes (i.e. the way it always was).

Although the first "fix" for this issue had a bug, it still needed to be applied. You all failed to see that there really is an issue here and troels was right to bring this up.
And Thomas, Rick, et al, it's not a MSVC-specific hack. If you think about it, it would have occurred with the test project troels attached even if it was using GNU autotools. Autotools do change the working dir to the file's dir also, like MSVC. This would fail there too, so to sum it up, this really was a C::B issue which got fixed the easiest way: let the user decide how to handle this.

I 'm not blaiming anyone here, I 'm just saying you failed to see the issue. After all, I was the one that told Thomas to close this bug report as invalid when he asked me what to do...
Title: Re: Moving to GCC question
Post by: troels on September 23, 2006, 10:26:18 pm
* Added compiler-independent option to explicitely add the currently compiling file's directory to the compiler's search dirs
Autotools do change the working dir to the file's dir also, like MSVC

After a good long time to think I cannot think of any reason not to do it like autotools and MSVC.

In other words, how about removing this option? Just make the default to change the dir to the compiling file's dir when compiling. This seems natural and would bring CB in step with the other tools.

Regards
Title: Re: Moving to GCC question
Post by: Ceniza on September 24, 2006, 07:07:40 am
Quote from: troels
After a good long time to think I cannot think of any reason not to do it like autotools and MSVC.

In other words, how about removing this option? Just make the default to change the dir to the compiling file's dir when compiling. This seems natural and would bring CB in step with the other tools.

The main reason it isn't implemented by default is because having an IDE that does that kind of tricks at your back is evil. Generalizing: any software that does that kind of things is evil.

As usual some people like it, some others not, that's why it's an option... and since it's really evil, it's disabled by default :D
Title: Re: Moving to GCC question
Post by: troels on September 24, 2006, 09:16:36 am
Just for the record:
This is not a about adding a dir to the include list (evil).
Think setcwd. This is about launching the compiler, gcc.exe or whichever, in the dir of the module to be compiled, instead of launching it in some mother dir (the .cbp dir).
Title: Re: Moving to GCC question
Post by: yop on September 24, 2006, 11:31:34 am
This would also save a few more annoying things like when building from Makefiles the compiler reports the errors/warnings from the build/sources dir and then when c::b handles them it searches for the file in the wrong path (clicking on the error does not open the respective file). This is kind of the standard way that any Makefile generator (qmake/bakefile/autotools) handles builds. I don't really know what advantages the current approach has so I might be missing something.
Title: Re: Moving to GCC question
Post by: takeshimiya on September 24, 2006, 08:40:33 pm
So now I understand, it's about

Code
cd WhereTheFileIs/file.cpp
gcc file.cpp
instead of
Code
gcc -IWhereTheFileIs file.cpp
right?

This makes sense, and it's valid not only for gcc or any other compiler, but for any language, build tool or interpreter.

troals: I'd say leave the option but change the default.
Title: Re: Moving to GCC question
Post by: troels on September 24, 2006, 09:10:49 pm
So now I understand, it's about
Code
cd WhereTheFileIs/file.cpp
gcc file.cpp
instead of
Code
gcc -IWhereTheFileIs file.cpp
right?

Yes! You are illustrating the issue much better than I.

To recapitulate:
This is what the option currently do albeit not by default:
Code
gcc -IWhereTheFileIs file.cpp

But this would be a nicer default behaviour as it is what other tools seems to be doing:
Code
cd WhereTheFileIs
gcc file.cpp
Title: Re: Moving to GCC question
Post by: troels on December 02, 2015, 01:55:56 pm
Maybe this option can be removed again
"Explicitly add currently compiling file's directory to compiler search dirs" (Build Options tab)
It seems that 'recent' versions of MinGW gcc itself adds the compiling file's directory now (since when?).
Ie, I can compile my projects now without this option set, and also the test in Reply#3 that used to fail in 2006 (www.trak.dk/gcctest.zip)

Using CodeBlocks 15.12 RC with bundled MinGW tdm-1.
Best regards
Title: Re: Moving to GCC question
Post by: MortenMacFly on December 03, 2015, 01:02:44 pm
Maybe this option can be removed again
As long as old compilers are in th use we shouldn't remove it. Its disabled by default and... well... "well hidden" so it should not harm. And anyways: If you enable it it won't cause any negative side-effects even if MinGW add it once again.