Gcc how does it work




















In CS, we will predominantly use Makefiles to compile, assemble, and link our code, but the Makefile runs gcc to do the work. This is just a quick overview on how to compile and run your own programs should you decide to do so without a Makefile.

The simplest way to run gcc is to provide gcc a list of. Note that you do not put header files. If the program compiled without errors or warnings, you don't get any output from gcc , and you will have a new file in your directory, called a.

To run this file, you need to tell the shell to run the file in the current directory, by using. We generally don't want our programs named a. Note: be careful not to accidentally input a runnable file name that is the same as your input file - something like:. On myth , your profile has been set up to catch the error and not compile it, leaving your source file in tact. This is not the case on many other Linux systems, so be careful! On other systems, GCC would overwrite your source file with the new executable.

One of the most common flags is the "optimization level" flag, -O uppercase 'o'. See the man page for gcc for more details on optimization or see here for more information about optimizations. We will use this standard in CS We will also use the -g flag, which allows us to use the debugger, gdb , to give us exact line numbers in our code when we run it. If you're interested in even more information about gcc , check out Section 1 of this Stanford Unix Programming Tools document, as well as the full gcc manual GNU.

Submit Lab Preferences. Using it you can, for instance, have all the new GCC programs installed as "new-gcc" and the like. You will still have to specify different --prefix options for new GCC and old GCC, because it is only the executable program names that are transformed.

The difference is that you as administrator do not have to set up symlinks, but must specify additional directories in your as a user PATH. Here is the option to prefix "new-" to the new GCC installed programs:. You can use --program-transform-name if you have multiple versions of GCC, and wish to be sure about which version you are invoking. So if you're installing GCC 2. This problem manifests itself by programs not finding shared libraries they depend on when the programs are started.

The short explanation is that if you always pass a -R option to the linker, your programs become dependent on directories which may be NFS mounted, and programs — even those which do not require these directories — may hang unnecessarily when an NFS server goes down.

SunOS effectively always passed a -R option for every -L option; this was a bad idea, and it was removed for Solaris. However, if you feel you really need such an option to be passed automatically to the linker, you may add it to a GCC specs file. Alternately the syntax -Wl,option can be used to ask GCC to transfer the flag option to the linker.

Since, on most platforms, the hard-coded list includes directories in which the system assembler and linker can be found, you may have to take one of the following actions to arrange that GCC uses the GNU versions of those programs. GCC 2. First look for an explicit '.

If you do not find an explicit '. Note that ':' at either the start or end of these variables is an implicit '. Either of these examples will run the tests three times. Once with -fPIC , once with -fpic , and once with no additional flags.

In order to make a specialization of a template function a friend of a possibly template class, you must explicitly state that the friend function is a template, by appending angle brackets to its name, and this template function must have been declared already. Here's an example:. The above declaration declares a non-template function named bar , so it must be explicitly defined for each specialization of foo.

A template definition of bar won't do, because it is unrelated with the non-template declaration above. So you'd have to end up writing:. If you meant bar to be a template function, you should have forward-declared it as follows. Note that, since the template function declaration refers to the template class, the template class must be forward-declared too:. In this case, the template argument list could be left empty, because it can be implicitly deduced from the function arguments, but the angle brackets must be present, otherwise the declaration will be taken as a non-template function.

Furthermore, in some cases, you may have to explicitly specify the template arguments, to remove ambiguity. This leads to better performance. The compiler has to emit them in any translation unit that requires their presence, and then rely on the linking and loading process to make sure that only one of them is active in the final executable. With static linking all of these symbols are resolved at link time, but with dynamic linking, further resolution occurs at load time.

You have to ensure that objects within a shared library are resolved against objects in the executable and other shared libraries. Template instantiations are another, user visible, case of objects with vague linkage, which needs similar resolution.



0コメント

  • 1000 / 1000