[14547] | 1 | |
---|
[18473] | 2 | GCC Frequently Asked Questions |
---|
| 3 | |
---|
| 4 | The latest version of this document is always available at |
---|
[20088] | 5 | [1]http://gcc.gnu.org/faq.html. |
---|
[18473] | 6 | |
---|
| 7 | This FAQ tries to answer specific questions concerning GCC. For |
---|
| 8 | general information regarding C, C++, resp. Fortran please check the |
---|
| 9 | [2]comp.lang.c FAQ, [3]comp.std.c++ FAQ, and the [4]Fortran |
---|
[16959] | 10 | Information page. |
---|
[18473] | 11 | |
---|
[16959] | 12 | Other GCC-related FAQs: [5]libstdc++-v3, and [6]GCJ. |
---|
[14547] | 13 | _________________________________________________________________ |
---|
[18473] | 14 | |
---|
[14547] | 15 | Questions |
---|
[18473] | 16 | |
---|
[16959] | 17 | 1. [7]General information |
---|
| 18 | 1. [8]What is the relationship between GCC and EGCS? |
---|
[20088] | 19 | 2. [9]What is an open development model? |
---|
| 20 | 3. [10]How do I get a bug fixed or a feature added? |
---|
| 21 | 4. [11]Does GCC work on my platform? |
---|
| 22 | 2. [12]Installation |
---|
| 23 | 1. [13]How to install multiple versions of GCC |
---|
| 24 | 2. [14]Dynamic linker is unable to find GCC libraries |
---|
| 25 | 3. [15]libstdc++/libio tests fail badly with --enable-shared |
---|
| 26 | 4. [16]GCC can not find GNU as/GNU ld |
---|
| 27 | 5. [17]cpp: Usage:... Error |
---|
| 28 | 6. [18]Optimizing the compiler itself |
---|
| 29 | 7. [19]Why does libiconv get linked into jc1 on Solaris? |
---|
| 30 | 3. [20]Testsuite problems |
---|
| 31 | 1. [21]How do I pass flags like -fnew-abi to the testsuite? |
---|
| 32 | 2. [22]How can I run the test suite with multiple options? |
---|
| 33 | 4. [23]Older versions of GCC |
---|
| 34 | 1. [24]Is there a stringstream / sstream for GCC 2.95.2? |
---|
| 35 | 5. [25]Miscellaneous |
---|
| 36 | 1. [26]Friend Templates |
---|
| 37 | 2. [27]dynamic_cast, throw, typeid don't work with shared |
---|
[16959] | 38 | libraries |
---|
[20088] | 39 | 3. [28]Why do I need autoconf, bison, xgettext, automake, etc? |
---|
| 40 | 4. [29]Why can't I build a shared library? |
---|
| 41 | 5. [30]When building C++, the linker says my constructors, |
---|
[18473] | 42 | destructors or virtual tables are undefined, but I defined |
---|
[14547] | 43 | them |
---|
[20088] | 44 | 6. [31]Will GCC someday include an incremental linker? |
---|
[14547] | 45 | _________________________________________________________________ |
---|
[18473] | 46 | |
---|
[14547] | 47 | General information |
---|
[18473] | 48 | |
---|
[16959] | 49 | What is the relationship between GCC and EGCS? |
---|
[14547] | 50 | |
---|
[18473] | 51 | In 1990/1991 gcc version 1 had reached a point of stability. For the |
---|
| 52 | targets it could support, it worked well. It had limitations inherent |
---|
| 53 | in its design that would be difficult to resolve, so a major effort |
---|
[20088] | 54 | was made to resolve those limitations and gcc version 2 was the |
---|
[14547] | 55 | result. |
---|
[18473] | 56 | |
---|
| 57 | When we had gcc2 in a useful state, development efforts on gcc1 |
---|
| 58 | stopped and we all concentrated on making gcc2 better than gcc1 could |
---|
| 59 | ever be. This is the kind of step forward we wanted to make with the |
---|
[14547] | 60 | EGCS project when it was formed in 1997. |
---|
[18473] | 61 | |
---|
| 62 | In April 1999 the Free Software Foundation officially halted |
---|
[14547] | 63 | development on the gcc2 compiler and appointed the EGCS project as the |
---|
[18473] | 64 | official GCC maintainers. The net result was a single project which |
---|
| 65 | carries forward GCC development under the ultimate control of the |
---|
[20088] | 66 | [32]GCC Steering Committee. |
---|
[14547] | 67 | _________________________________________________________________ |
---|
[18473] | 68 | |
---|
[14547] | 69 | What is an open development model? |
---|
| 70 | |
---|
[20088] | 71 | We are using a bazaar style [33][1] approach to GCC development: we |
---|
[18473] | 72 | make snapshots publicly available to anyone who wants to try them; we |
---|
| 73 | welcome anyone to join the development mailing list. All of the |
---|
[16959] | 74 | discussions on the development mailing list are available via the web. |
---|
[18473] | 75 | We're going to be making releases with a much higher frequency than |
---|
[16959] | 76 | they have been made in the past. |
---|
[18473] | 77 | |
---|
| 78 | In addition to weekly snapshots of the GCC development sources, we |
---|
| 79 | have the sources readable from a CVS server by anyone. Furthermore we |
---|
| 80 | are using remote CVS to allow remote maintainers write access to the |
---|
[14547] | 81 | sources. |
---|
[18473] | 82 | |
---|
| 83 | There have been many potential GCC developers who were not able to |
---|
| 84 | participate in GCC development in the past. We want these people to |
---|
| 85 | help in any way they can; we ultimately want GCC to be the best |
---|
[14547] | 86 | compiler in the world. |
---|
[18473] | 87 | |
---|
| 88 | A compiler is a complicated piece of software, there will still be |
---|
| 89 | strong central maintainers who will reject patches, who will demand |
---|
| 90 | documentation of implementations, and who will keep the level of |
---|
| 91 | quality as high as it is today. Code that could use wider testing may |
---|
[14547] | 92 | be integrated--code that is simply ill-conceived won't be. |
---|
[18473] | 93 | |
---|
| 94 | GCC is not the first piece of software to use this open development |
---|
| 95 | process; FreeBSD, the Emacs lisp repository, and the Linux kernel are |
---|
[14547] | 96 | a few examples of the bazaar style of development. |
---|
[18473] | 97 | |
---|
| 98 | With GCC, we are adding new features and optimizations at a rate that |
---|
| 99 | has not been done since the creation of gcc2; these additions |
---|
| 100 | inevitably have a temporarily destabilizing effect. With the help of |
---|
| 101 | developers working together with this bazaar style development, the |
---|
| 102 | resulting stability and quality levels will be better than we've had |
---|
[14547] | 103 | before. |
---|
[18473] | 104 | |
---|
| 105 | [1] We've been discussing different development models a lot over |
---|
[14547] | 106 | the past few months. The paper which started all of this introduced |
---|
[18473] | 107 | two terms: A cathedral development model versus a bazaar |
---|
| 108 | development model. The paper is written by Eric S. Raymond, it is |
---|
[20088] | 109 | called ``The Cathedral and the Bazaar''. The paper is a useful |
---|
[14547] | 110 | starting point for discussions. |
---|
| 111 | _________________________________________________________________ |
---|
[18473] | 112 | |
---|
[14547] | 113 | How do I get a bug fixed or a feature added? |
---|
| 114 | |
---|
[18473] | 115 | There are lots of ways to get something fixed. The list below may be |
---|
| 116 | incomplete, but it covers many of the common cases. These are listed |
---|
[20088] | 117 | roughly in order of decreasing difficulty for the average GCC user, |
---|
[18473] | 118 | meaning someone who is not skilled in the internals of GCC, and where |
---|
| 119 | difficulty is measured in terms of the time required to fix the bug. |
---|
| 120 | No alternative is better than any other; each has its benefits and |
---|
[14547] | 121 | disadvantages. |
---|
[18473] | 122 | * Fix it yourself. This alternative will probably bring results, if |
---|
| 123 | you work hard enough, but will probably take a lot of time, and, |
---|
| 124 | depending on the quality of your work and the perceived benefits |
---|
| 125 | of your changes, your code may or may not ever make it into an |
---|
[14547] | 126 | official release of GCC. |
---|
[20088] | 127 | * [34]Report the problem to the GCC bug tracking system and hope |
---|
| 128 | that someone will be kind enough to fix it for you. While this is |
---|
| 129 | certainly possible, and often happens, there is no guarantee that |
---|
| 130 | it will. You should not expect the same response from this method |
---|
| 131 | that you would see from a commercial support organization since |
---|
| 132 | the people who read GCC bug reports, if they choose to help you, |
---|
| 133 | will be volunteering their time. |
---|
| 134 | * Hire someone to fix it for you. There are various companies and |
---|
| 135 | individuals providing support for GCC. This alternative costs |
---|
| 136 | money, but is relatively likely to get results. |
---|
[14547] | 137 | _________________________________________________________________ |
---|
[16959] | 138 | |
---|
| 139 | Does GCC work on my platform? |
---|
| 140 | |
---|
[18473] | 141 | The host/target specific installation notes for GCC include |
---|
| 142 | information about known problems with installing or using GCC on |
---|
| 143 | particular platforms. These are included in the sources for a release |
---|
[20088] | 144 | in INSTALL/specific.html, and the [35]latest version is always |
---|
| 145 | available at the GCC web site. Reports of [36]successful builds for |
---|
[18473] | 146 | several versions of GCC are also available at the web site. |
---|
[16959] | 147 | _________________________________________________________________ |
---|
[18473] | 148 | |
---|
[14547] | 149 | Installation |
---|
| 150 | |
---|
[16959] | 151 | How to install multiple versions of GCC |
---|
[14547] | 152 | |
---|
[18473] | 153 | It may be desirable to install multiple versions of the compiler on |
---|
| 154 | the same system. This can be done by using different prefix paths at |
---|
[14547] | 155 | configure time and a few symlinks. |
---|
[18473] | 156 | |
---|
| 157 | Basically, configure the two compilers with different --prefix |
---|
| 158 | options, then build and install each compiler. Assume you want "gcc" |
---|
[14547] | 159 | to be the latest compiler and available in /usr/local/bin; also assume |
---|
[18473] | 160 | that you want "gcc2" to be the older gcc2 compiler and also available |
---|
[14547] | 161 | in /usr/local/bin. |
---|
[18473] | 162 | |
---|
| 163 | The easiest way to do this is to configure the new GCC with |
---|
| 164 | --prefix=/usr/local/gcc and the older gcc2 with |
---|
| 165 | --prefix=/usr/local/gcc2. Build and install both compilers. Then make |
---|
| 166 | a symlink from /usr/local/bin/gcc to /usr/local/gcc/bin/gcc and from |
---|
| 167 | /usr/local/bin/gcc2 to /usr/local/gcc2/bin/gcc. Create similar links |
---|
[14547] | 168 | for the "g++", "c++" and "g77" compiler drivers. |
---|
[18473] | 169 | |
---|
| 170 | An alternative to using symlinks is to configure with a |
---|
| 171 | --program-transform-name option. This option specifies a sed command |
---|
| 172 | to process installed program names with. Using it you can, for |
---|
[14547] | 173 | instance, have all the new GCC programs installed as "new-gcc" and the |
---|
[18473] | 174 | like. You will still have to specify different --prefix options for |
---|
| 175 | new GCC and old GCC, because it is only the executable program names |
---|
[14547] | 176 | that are transformed. The difference is that you (as administrator) do |
---|
[18473] | 177 | not have to set up symlinks, but must specify additional directories |
---|
[14547] | 178 | in your (as a user) PATH. A complication with --program-transform-name |
---|
[18473] | 179 | is that the sed command invariably contains characters significant to |
---|
| 180 | the shell, and these have to be escaped correctly, also it is not |
---|
| 181 | possible to use "^" or "$" in the command. Here is the option to |
---|
[16959] | 182 | prefix "new-" to the new GCC installed programs: |
---|
[18473] | 183 | |
---|
[16959] | 184 | --program-transform-name='s,\\\\(.*\\\\),new-\\\\1,' |
---|
[18473] | 185 | |
---|
[16959] | 186 | With the above --prefix option, that will install the new GCC programs |
---|
[18473] | 187 | into /usr/local/gcc/bin with names prefixed by "new-". You can use |
---|
| 188 | --program-transform-name if you have multiple versions of GCC, and |
---|
[14547] | 189 | wish to be sure about which version you are invoking. |
---|
[18473] | 190 | |
---|
| 191 | If you use --prefix, GCC may have difficulty locating a GNU assembler |
---|
[20088] | 192 | or linker on your system, [37]GCC can not find GNU as/GNU ld explains |
---|
[14547] | 193 | how to deal with this. |
---|
[18473] | 194 | |
---|
| 195 | Another option that may be easier is to use the --program-prefix= or |
---|
| 196 | --program-suffix= options to configure. So if you're installing GCC |
---|
| 197 | 2.95.2 and don't want to disturb the current version of GCC in |
---|
[16959] | 198 | /usr/local/bin/, you could do |
---|
[18473] | 199 | |
---|
[16959] | 200 | configure --program-suffix=-2.95.2 <other configure options> |
---|
[18473] | 201 | |
---|
[16959] | 202 | This should result in GCC being installed as /usr/local/bin/gcc-2.95.2 |
---|
| 203 | instead of /usr/local/bin/gcc. |
---|
[14547] | 204 | _________________________________________________________________ |
---|
[18473] | 205 | |
---|
[14547] | 206 | Dynamic linker is unable to find GCC libraries |
---|
| 207 | |
---|
| 208 | This problem manifests itself by programs not finding shared libraries |
---|
[18473] | 209 | they depend on when the programs are started. Note this problem often |
---|
| 210 | manifests itself with failures in the libio/libstdc++ tests after |
---|
[14547] | 211 | configuring with --enable-shared and building GCC. |
---|
[18473] | 212 | |
---|
| 213 | GCC does not specify a runpath so that the dynamic linker can find |
---|
[14547] | 214 | dynamic libraries at runtime. |
---|
[18473] | 215 | |
---|
| 216 | The short explanation is that if you always pass a -R option to the |
---|
| 217 | linker, then your programs become dependent on directories which may |
---|
[14547] | 218 | be NFS mounted, and programs may hang unnecessarily when an NFS server |
---|
| 219 | goes down. |
---|
[18473] | 220 | |
---|
| 221 | The problem is not programs that do require the directories; those |
---|
| 222 | programs are going to hang no matter what you do. The problem is |
---|
[14547] | 223 | programs that do not require the directories. |
---|
[18473] | 224 | |
---|
| 225 | SunOS effectively always passed a -R option for every -L option; this |
---|
| 226 | was a bad idea, and so it was removed for Solaris. We should not |
---|
[14547] | 227 | recreate it. |
---|
[18473] | 228 | |
---|
| 229 | However, if you feel you really need such an option to be passed |
---|
| 230 | automatically to the linker, you may add it to the GCC specs file. |
---|
| 231 | This file can be found in the same directory that contains cc1 (run |
---|
[14547] | 232 | gcc -print-prog-name=cc1 to find it). You may add linker flags such as |
---|
[18473] | 233 | -R or -rpath, depending on platform and linker, to the *link or *lib |
---|
[14547] | 234 | specs. |
---|
[18473] | 235 | |
---|
| 236 | Another alternative is to install a wrapper script around gcc, g++ or |
---|
| 237 | ld that adds the appropriate directory to the environment variable |
---|
[14547] | 238 | LD_RUN_PATH or equivalent (again, it's platform-dependent). |
---|
[18473] | 239 | |
---|
[14547] | 240 | Yet another option, that works on a few platforms, is to hard-code the |
---|
[18473] | 241 | full pathname of the library into its soname. This can only be |
---|
| 242 | accomplished by modifying the appropriate .ml file within |
---|
[14547] | 243 | libstdc++/config (and also libg++/config, if you are building libg++), |
---|
[18473] | 244 | so that $(libdir)/ appears just before the library name in -soname or |
---|
[14547] | 245 | -h options. |
---|
| 246 | _________________________________________________________________ |
---|
[18473] | 247 | |
---|
[14547] | 248 | GCC can not find GNU as/GNU ld |
---|
| 249 | |
---|
[18473] | 250 | GCC searches the PATH for an assembler and a loader, but it only does |
---|
[16959] | 251 | so after searching a directory list hard-coded in the GCC executables. |
---|
[18473] | 252 | Since, on most platforms, the hard-coded list includes directories in |
---|
[20088] | 253 | which the system assembler and loader can be found, you may have to |
---|
[18473] | 254 | take one of the following actions to arrange that GCC uses the GNU |
---|
[14547] | 255 | versions of those programs. |
---|
[18473] | 256 | |
---|
[14547] | 257 | To ensure that GCC finds the GNU assembler (the GNU loader), which are |
---|
[20088] | 258 | required by [38]some configurations, you should configure these with |
---|
[14547] | 259 | the same --prefix option as you used for GCC. Then build & install GNU |
---|
| 260 | as (GNU ld) and proceed with building GCC. |
---|
[18473] | 261 | |
---|
| 262 | Another alternative is to create links to GNU as and ld in any of the |
---|
| 263 | directories printed by the command `gcc -print-search-dirs | grep |
---|
| 264 | '^programs:''. The link to `ld' should be named `real-ld' if `ld' |
---|
[14547] | 265 | already exists. If such links do not exist while you're compiling GCC, |
---|
[18473] | 266 | you may have to create them in the build directories too, within the |
---|
[16959] | 267 | gcc directory and in all the gcc/stage* subdirectories. |
---|
[18473] | 268 | |
---|
| 269 | GCC 2.95 allows you to specify the full pathname of the assembler and |
---|
[14547] | 270 | the linker to use. The configure flags are `--with-as=/path/to/as' and |
---|
[18473] | 271 | `--with-ld=/path/to/ld'. GCC will try to use these pathnames before |
---|
| 272 | looking for `as' or `(real-)ld' in the standard search dirs. If, at |
---|
| 273 | configure-time, the specified programs are found to be GNU utilities, |
---|
[14547] | 274 | `--with-gnu-as' and `--with-gnu-ld' need not be used; these flags will |
---|
[18473] | 275 | be auto-detected. One drawback of this option is that it won't allow |
---|
| 276 | you to override the search path for assembler and linker with |
---|
[14547] | 277 | command-line options -B/path/ if the specified filenames exist. |
---|
| 278 | _________________________________________________________________ |
---|
[18473] | 279 | |
---|
[14547] | 280 | cpp: Usage:... Error |
---|
| 281 | |
---|
[18473] | 282 | If you get an error like this when building GCC (particularly when |
---|
| 283 | building __mulsi3), then you likely have a problem with your |
---|
[14547] | 284 | environment variables. |
---|
| 285 | cpp: Usage: /usr/lib/gcc-lib/i586-unknown-linux-gnulibc1/2.7.2.3/cpp |
---|
| 286 | [switches] input output |
---|
| 287 | |
---|
[18473] | 288 | First look for an explicit '.' in either LIBRARY_PATH or |
---|
| 289 | GCC_EXEC_PREFIX from your environment. If you do not find an explicit |
---|
| 290 | '.', look for an empty pathname in those variables. Note that ':' at |
---|
[14547] | 291 | either the start or end of these variables is an implicit '.' and will |
---|
| 292 | cause problems. |
---|
[18473] | 293 | |
---|
[14547] | 294 | Also note '::' in these paths will also cause similar problems. |
---|
| 295 | _________________________________________________________________ |
---|
[18473] | 296 | |
---|
[16959] | 297 | Optimizing the compiler itself |
---|
[14547] | 298 | |
---|
[18473] | 299 | If you want to test a particular optimization option, it's useful to |
---|
| 300 | try bootstrapping the compiler with that option turned on. For |
---|
[16959] | 301 | example, to test the -fssa option, you could bootstrap like this: |
---|
| 302 | make BOOT_CFLAGS="-O2 -fssa" bootstrap |
---|
[14547] | 303 | _________________________________________________________________ |
---|
[18473] | 304 | |
---|
[20088] | 305 | Why does libiconv get linked into jc1 on Solaris? |
---|
[18473] | 306 | |
---|
[20088] | 307 | The Java front end requires iconv. If the compiler used to bootstrap |
---|
| 308 | GCC finds libiconv (because the GNU version of libiconv has been |
---|
| 309 | installed in the same prefix as the bootstrap compiler), but the newly |
---|
| 310 | built GCC does not find the library (because it will be installed with |
---|
| 311 | a different prefix), then a link-time error will occur when building |
---|
| 312 | jc1. This problem does not show up so often on platforms that have |
---|
| 313 | libiconv in a default location (like /usr/lib) because then both |
---|
| 314 | compilers can find a library named libiconv, even though it is a |
---|
| 315 | different library. |
---|
[14547] | 316 | |
---|
[20088] | 317 | Using --disable-nls at configure-time does not prevent this problem |
---|
| 318 | because jc1 uses iconv even in that case. Solutions include |
---|
| 319 | temporarily removing the GNU libiconv, copying it to a default |
---|
| 320 | location such as /usr/lib/, and using --enable-languages at |
---|
| 321 | configure-time to disable Java. |
---|
[14547] | 322 | _________________________________________________________________ |
---|
[18473] | 323 | |
---|
[20088] | 324 | Testsuite problems |
---|
| 325 | |
---|
[14547] | 326 | How do I pass flags like -fnew-abi to the testsuite? |
---|
| 327 | |
---|
[18473] | 328 | If you invoke runtest directly, you can use the --tool_opts option, |
---|
[14547] | 329 | e.g: |
---|
| 330 | runtest --tool_opts "-fnew-abi -fno-honor-std" <other options> |
---|
| 331 | |
---|
[18473] | 332 | Or, if you use make check you can use the make variable RUNTESTFLAGS, |
---|
[14547] | 333 | e.g: |
---|
[16959] | 334 | make RUNTESTFLAGS="--tool_opts '-fnew-abi -fno-honor-std'" check-g++ |
---|
[14547] | 335 | _________________________________________________________________ |
---|
[18473] | 336 | |
---|
[14547] | 337 | How can I run the test suite with multiple options? |
---|
| 338 | |
---|
| 339 | If you invoke runtest directly, you can use the --target_board option, |
---|
| 340 | e.g: |
---|
| 341 | runtest --target_board "unix{-fPIC,-fpic,}" <other options> |
---|
| 342 | |
---|
[18473] | 343 | Or, if you use make check you can use the make variable RUNTESTFLAGS, |
---|
[14547] | 344 | e.g: |
---|
[16959] | 345 | make RUNTESTFLAGS="--target_board 'unix{-fPIC,-fpic,}'" check-gcc |
---|
[14547] | 346 | |
---|
[18473] | 347 | Either of these examples will run the tests three times. Once with |
---|
[14547] | 348 | -fPIC, once with -fpic, and once with no additional flags. |
---|
[18473] | 349 | |
---|
[14547] | 350 | This technique is particularly useful on multilibbed targets. |
---|
| 351 | _________________________________________________________________ |
---|
[18473] | 352 | |
---|
[16959] | 353 | Older versions of GCC and EGCS |
---|
[18473] | 354 | |
---|
[16959] | 355 | Is there a stringstream / sstream for GCC 2.95.2? |
---|
[14547] | 356 | |
---|
[16959] | 357 | Yes, it's at: |
---|
[20088] | 358 | [39]http://gcc.gnu.org/ml/libstdc++/2000-q2/msg00700/sstream. |
---|
[14547] | 359 | _________________________________________________________________ |
---|
[18473] | 360 | |
---|
[14547] | 361 | Miscellaneous |
---|
[18473] | 362 | |
---|
[14547] | 363 | Friend Templates |
---|
| 364 | |
---|
| 365 | In order to make a specialization of a template function a friend of a |
---|
[18473] | 366 | (possibly template) class, you must explicitly state that the friend |
---|
| 367 | function is a template, by appending angle brackets to its name, and |
---|
| 368 | this template function must have been declared already. Here's an |
---|
[14547] | 369 | example: |
---|
| 370 | template <typename T> class foo { |
---|
| 371 | friend void bar(foo<T>); |
---|
| 372 | } |
---|
| 373 | |
---|
[18473] | 374 | The above declaration declares a non-template function named bar, so |
---|
| 375 | it must be explicitly defined for each specialization of foo. A |
---|
| 376 | template definition of bar won't do, because it is unrelated with the |
---|
[14547] | 377 | non-template declaration above. So you'd have to end up writing: |
---|
| 378 | void bar(foo<int>) { /* ... */ } |
---|
| 379 | void bar(foo<void>) { /* ... */ } |
---|
| 380 | |
---|
[18473] | 381 | If you meant bar to be a template function, you should have |
---|
[14547] | 382 | forward-declared it as follows. Note that, since the template function |
---|
[18473] | 383 | declaration refers to the template class, the template class must be |
---|
[14547] | 384 | forward-declared too: |
---|
| 385 | template <typename T> |
---|
| 386 | class foo; |
---|
| 387 | |
---|
| 388 | template <typename T> |
---|
| 389 | void bar(foo<T>); |
---|
| 390 | |
---|
| 391 | template <typename T> |
---|
| 392 | class foo { |
---|
| 393 | friend void bar<>(foo<T>); |
---|
| 394 | }; |
---|
| 395 | |
---|
| 396 | template <typename T> |
---|
| 397 | void bar(foo<T>) { /* ... */ } |
---|
| 398 | |
---|
[18473] | 399 | In this case, the template argument list could be left empty, because |
---|
| 400 | it can be implicitly deduced from the function arguments, but the |
---|
| 401 | angle brackets must be present, otherwise the declaration will be |
---|
| 402 | taken as a non-template function. Furthermore, in some cases, you may |
---|
| 403 | have to explicitly specify the template arguments, to remove |
---|
[14547] | 404 | ambiguity. |
---|
[18473] | 405 | |
---|
[14547] | 406 | An error in the last public comment draft of the ANSI/ISO C++ Standard |
---|
[18473] | 407 | and the fact that previous releases of GCC would accept such friend |
---|
| 408 | declarations as template declarations has led people to believe that |
---|
[14547] | 409 | the forward declaration was not necessary, but, according to the final |
---|
| 410 | version of the Standard, it is. |
---|
| 411 | _________________________________________________________________ |
---|
[18473] | 412 | |
---|
[16959] | 413 | dynamic_cast, throw, typeid don't work with shared libraries |
---|
[14547] | 414 | |
---|
[16959] | 415 | The new C++ ABI in the GCC 3.0 series uses address comparisons, rather |
---|
| 416 | than string compares, to determine type equality. This leads to better |
---|
[18473] | 417 | performance. Like other objects that have to be present in the final |
---|
[21198] | 418 | executable, these std::type_info objects have what is called vague |
---|
[18473] | 419 | linkage because they are not tightly bound to any one particular |
---|
| 420 | translation unit (object file). The compiler has to emit them in any |
---|
| 421 | translation unit that requires their presence, and then rely on the |
---|
| 422 | linking and loading process to make sure that only one of them is |
---|
| 423 | active in the final executable. With static linking all of these |
---|
| 424 | symbols are resolved at link time, but with dynamic linking, further |
---|
[16959] | 425 | resolution occurs at load time. You have to ensure that objects within |
---|
[18473] | 426 | a shared library are resolved against objects in the executable and |
---|
[16959] | 427 | other shared libraries. |
---|
[18473] | 428 | * For a program which is linked against a shared library, no |
---|
[21198] | 429 | additional precautions are needed. |
---|
[18473] | 430 | * You cannot create a shared library with the "-Bsymbolic" option, |
---|
[16959] | 431 | as that prevents the resolution described above. |
---|
[18473] | 432 | * If you use dlopen to explicitly load code from a shared library, |
---|
| 433 | you must do several things. First, export global symbols from the |
---|
| 434 | executable by linking it with the "-E" flag (you will have to |
---|
| 435 | specify this as "-Wl,-E" if you are invoking the linker in the |
---|
| 436 | usual manner from the compiler driver, g++). You must also make |
---|
| 437 | the external symbols in the loaded library available for |
---|
| 438 | subsequent libraries by providing the RTLD_GLOBAL flag to dlopen. |
---|
[16959] | 439 | The symbol resolution can be immediate or lazy. |
---|
[18473] | 440 | |
---|
| 441 | Template instantiations are another, user visible, case of objects |
---|
[16959] | 442 | with vague linkage, which needs similar resolution. If you do not take |
---|
[18473] | 443 | the above precautions, you may discover that a template instantiation |
---|
| 444 | with the same argument list, but instantiated in multiple translation |
---|
| 445 | units, has several addresses, depending in which translation unit the |
---|
| 446 | address is taken. (This is not an exhaustive list of the kind of |
---|
| 447 | objects which have vague linkage and are expected to be resolved |
---|
[16959] | 448 | during linking & loading.) |
---|
[18473] | 449 | |
---|
| 450 | If you are worried about different objects with the same name |
---|
| 451 | colliding during the linking or loading process, then you should use |
---|
| 452 | namespaces to disambiguate them. Giving distinct objects with global |
---|
| 453 | linkage the same name is a violation of the One Definition Rule (ODR) |
---|
[16959] | 454 | [basic.def.odr]. |
---|
[18473] | 455 | |
---|
[16959] | 456 | For more details about the way that GCC implements these and other C++ |
---|
[20088] | 457 | features, please read the [40]ABI specification. Note the |
---|
[21198] | 458 | std::type_info objects which must be resolved all begin with "_ZTS". |
---|
[18473] | 459 | Refer to ld's documentation for a description of the "-E" & |
---|
[16959] | 460 | "-Bsymbolic" flags. |
---|
[14547] | 461 | _________________________________________________________________ |
---|
[18473] | 462 | |
---|
[16959] | 463 | Why do I need autoconf, bison, xgettext, automake, etc? |
---|
[14547] | 464 | |
---|
[18473] | 465 | If you're using diffs up dated from one snapshot to the next, or if |
---|
| 466 | you're using the CVS repository, you may need several additional |
---|
[14547] | 467 | programs to build GCC. |
---|
[18473] | 468 | |
---|
| 469 | These include, but are not necessarily limited to autoconf, automake, |
---|
[14547] | 470 | bison, and xgettext. |
---|
[18473] | 471 | |
---|
| 472 | This is necessary because neither diff nor cvs keep timestamps |
---|
| 473 | correct. This causes problems for generated files as "make" may think |
---|
[14547] | 474 | those generated files are out of date and try to regenerate them. |
---|
[18473] | 475 | |
---|
| 476 | An easy way to work around this problem is to use the gcc_update |
---|
| 477 | script in the contrib subdirectory of GCC, which handles this |
---|
| 478 | transparently without requiring installation of any additional tools. |
---|
[14547] | 479 | (Note: Up to and including GCC 2.95 this script was called egcs_update |
---|
| 480 | .) |
---|
[18473] | 481 | |
---|
| 482 | When building from diffs or CVS or if you modified some sources, you |
---|
[14547] | 483 | may also need to obtain development versions of some GNU tools, as the |
---|
[18473] | 484 | production versions do not necessarily handle all features needed to |
---|
[14547] | 485 | rebuild GCC. |
---|
[18473] | 486 | |
---|
| 487 | In general, the current versions of these tools from |
---|
[20088] | 488 | [41]ftp://ftp.gnu.org/gnu/ will work. At present, Autoconf 2.50 is not |
---|
[16959] | 489 | supported, and you will need to use Autoconf 2.13; work is in progress |
---|
| 490 | to fix this problem. Also look at |
---|
[20088] | 491 | [42]ftp://gcc.gnu.org/pub/gcc/infrastructure/ for any special versions |
---|
[16959] | 492 | of packages. |
---|
[14547] | 493 | _________________________________________________________________ |
---|
[18473] | 494 | |
---|
[14547] | 495 | Why can't I build a shared library? |
---|
| 496 | |
---|
[18473] | 497 | When building a shared library you may get an error message from the |
---|
[14547] | 498 | linker like `assert pure-text failed:' or `DP relative code in file'. |
---|
[18473] | 499 | |
---|
| 500 | This kind of error occurs when you've failed to provide proper flags |
---|
[14547] | 501 | to gcc when linking the shared library. |
---|
[18473] | 502 | |
---|
[14547] | 503 | You can get this error even if all the .o files for the shared library |
---|
[18473] | 504 | were compiled with the proper PIC option. When building a shared |
---|
| 505 | library, gcc will compile additional code to be included in the |
---|
| 506 | library. That additional code must also be compiled with the proper |
---|
[14547] | 507 | PIC option. |
---|
[18473] | 508 | |
---|
| 509 | Adding the proper PIC option (-fpic or -fPIC) to the link line which |
---|
| 510 | creates the shared library will fix this problem on targets that |
---|
[14547] | 511 | support PIC in this manner. For example: |
---|
| 512 | gcc -c -fPIC myfile.c |
---|
| 513 | gcc -shared -o libmyfile.so -fPIC myfile.o |
---|
| 514 | _________________________________________________________________ |
---|
[18473] | 515 | |
---|
[14547] | 516 | When building C++, the linker says my constructors, destructors or virtual |
---|
| 517 | tables are undefined, but I defined them |
---|
| 518 | |
---|
[18473] | 519 | The ISO C++ Standard specifies that all virtual methods of a class |
---|
| 520 | that are not pure-virtual must be defined, but does not require any |
---|
| 521 | diagnostic for violations of this rule [class.virtual]/8. Based on |
---|
| 522 | this assumption, GCC will only emit the implicitly defined |
---|
| 523 | constructors, the assignment operator, the destructor and the virtual |
---|
| 524 | table of a class in the translation unit that defines its first such |
---|
[14547] | 525 | non-inline method. |
---|
[18473] | 526 | |
---|
| 527 | Therefore, if you fail to define this particular method, the linker |
---|
| 528 | may complain about the lack of definitions for apparently unrelated |
---|
| 529 | symbols. Unfortunately, in order to improve this error message, it |
---|
| 530 | might be necessary to change the linker, and this can't always be |
---|
[14547] | 531 | done. |
---|
[18473] | 532 | |
---|
| 533 | The solution is to ensure that all virtual methods that are not pure |
---|
| 534 | are defined. Note that a destructor must be defined even if it is |
---|
[14547] | 535 | declared pure-virtual [class.dtor]/7. |
---|
| 536 | _________________________________________________________________ |
---|
[18473] | 537 | |
---|
[16959] | 538 | Will GCC someday include an incremental linker? |
---|
[14547] | 539 | |
---|
[18473] | 540 | Incremental linking is part of the linker, not the compiler. As such, |
---|
[16959] | 541 | GCC doesn't have anything to do with incremental linking. Depending on |
---|
[18473] | 542 | what platform you use, it may be possible to tell GCC to use the |
---|
[16959] | 543 | platform's native linker (e.g., Solaris' ild(1)). |
---|
[14547] | 544 | |
---|
| 545 | References |
---|
| 546 | |
---|
[20088] | 547 | 1. http://gcc.gnu.org/faq.html |
---|
[14547] | 548 | 2. http://www.eskimo.com/~scs/C-faq/top.html |
---|
[20088] | 549 | 3. http://www.jamesd.demon.co.uk/csc/faq.html |
---|
[16959] | 550 | 4. http://www.fortran.com/fortran/info.html |
---|
| 551 | 5. http://gcc.gnu.org/onlinedocs/libstdc++/faq/index.html |
---|
| 552 | 6. http://gcc.gnu.org/java/faq.html |
---|
| 553 | 7. http://gcc.gnu.org/faq.html#general |
---|
| 554 | 8. http://gcc.gnu.org/faq.html#gcc |
---|
[20088] | 555 | 9. http://gcc.gnu.org/faq.html#open-development |
---|
| 556 | 10. http://gcc.gnu.org/faq.html#support |
---|
| 557 | 11. http://gcc.gnu.org/faq.html#platforms |
---|
| 558 | 12. http://gcc.gnu.org/faq.html#installation |
---|
| 559 | 13. http://gcc.gnu.org/faq.html#multiple |
---|
| 560 | 14. http://gcc.gnu.org/faq.html#rpath |
---|
| 561 | 15. http://gcc.gnu.org/faq.html#rpath |
---|
| 562 | 16. http://gcc.gnu.org/faq.html#gas |
---|
| 563 | 17. http://gcc.gnu.org/faq.html#environ |
---|
| 564 | 18. http://gcc.gnu.org/faq.html#optimizing |
---|
| 565 | 19. http://gcc.gnu.org/faq.html#iconv |
---|
| 566 | 20. http://gcc.gnu.org/faq.html#testsuite |
---|
| 567 | 21. http://gcc.gnu.org/faq.html#testoptions |
---|
| 568 | 22. http://gcc.gnu.org/faq.html#multipletests |
---|
| 569 | 23. http://gcc.gnu.org/faq.html#old |
---|
| 570 | 24. http://gcc.gnu.org/faq.html#2.95sstream |
---|
| 571 | 25. http://gcc.gnu.org/faq.html#misc |
---|
| 572 | 26. http://gcc.gnu.org/faq.html#friend |
---|
| 573 | 27. http://gcc.gnu.org/faq.html#dso |
---|
| 574 | 28. http://gcc.gnu.org/faq.html#generated_files |
---|
| 575 | 29. http://gcc.gnu.org/faq.html#picflag-needed |
---|
| 576 | 30. http://gcc.gnu.org/faq.html#vtables |
---|
| 577 | 31. http://gcc.gnu.org/faq.html#incremental |
---|
| 578 | 32. http://gcc.gnu.org/steering.html |
---|
| 579 | 33. http://gcc.gnu.org/faq.html#cathedral-vs-bazaar |
---|
| 580 | 34. http://gcc.gnu.org/bugs.html |
---|
| 581 | 35. http://gcc.gnu.org/install/specific.html |
---|
| 582 | 36. http://gcc.gnu.org/buildstat.html |
---|
| 583 | 37. http://gcc.gnu.org/faq.html#gas |
---|
| 584 | 38. http://gcc.gnu.org/install/specific.html |
---|
| 585 | 39. http://gcc.gnu.org/ml/libstdc++/2000-q2/msg00700/sstream |
---|
| 586 | 40. http://www.codesourcery.com/cxx-abi/ |
---|
| 587 | 41. ftp://ftp.gnu.org/gnu/ |
---|
| 588 | 42. ftp://gcc.gnu.org/pub/gcc/infrastructure/ |
---|