FAQ-Compiling (errors)

From Code::Blocks
Revision as of 18:16, 12 March 2013 by BlueHazzard (talk | contribs)

Return to FAQ.


Q: How do I troubleshoot a compiler problem?

A: I would start by turning on full Compiler logging.

This is done by selecting the "Full command line" option Under menu "Settings" -> "Compiler" -> Global compiler settings -> [the compiler you use] -> "Other Setting" tab, "Compiler logging". In 12.11 and newer this is enabled by default.

This option will make Code::Blocks output the exact commands it uses to compile your code.

Things to remember:

  • You should review all the commands and their options;
  • If you have compiled your app before, do a re-build (or clean before build) to see all compiling / linking steps;
  • If you don't know what an option or a command does please read the documentation for the compiler/linker you're using;
  • Look for missing commands;
  • For every source file (.cpp; .c; .d; etc) in your project, you must have at least one command in the log. This command must produce an object file (file extension .o if using gcc/g++ and .obj if using Visual Studio);
  • Every object file should be linked in the final executable, if not there are undefined symbols errors;
  • Remember the file extension matters: *.c is compiled as C file, *.cpp is compiled as C++ file. Read more

Q: What do I need to know when using 3rd party libs?

Here are some basics about typical mistakes done when working with third party libs, including wxWidgets. The following is valid for every third party SDK / toolbox / component you want to use and describes what steps your have to do:

  • Download the sources of the component OR a ready-to-use development version. The difference: While the first requires you to compile the component yourself is will definitely work with your compiler. The latter must be compiled in a compatible way: So a compatible compiler, compatible OS, compatible settings. Inspect the components docs about how to get what you want.
  • Place the component sources and compiled parts anywhere you want It is not required to copy such parts to any other folder you might think - in fact, this may even be dangerous in case you overwrite existing files.
  • Create a project where you want to use your component.
  • In the compiler settings, point to the folder, where the include files of your component are. For WX this is special, as usually you include like #include <wx/foo.h>. So do not point to [Component_Folder]\include\wx, but to [Component_Folder]\include instead.
  • Note that the compiler only needs to know the interfaces / classes / structures / methods, it will not throw an error about undefined references or alike. The compiler will only complain in case it cannot find references in terms of include files. If thats the case, adjust your project's compiler settings. Keep in mind that you do need to fulfil the requirements of your component itself, too. Thus, wxChart for example will need for wxWidgets, too. So - you may need to do the same process for wxWidgets, too before you can use wxChart - unless you have done that already.
  • In the linker settings, point to the folder where you have your compiled library. A library usually ends with *.a or *.lib. Note that there are generally two types of libs: Static libs (after linking you are done) and Dynamic libs (where you link against an import lib but require another dynamic lib at runtime).
  • In the linker settings add the library/libraries you need to link against in the right order to the list of libs to link against. Order matters - again, dependencies must be taken into account. Inspect the developers guide of the component to know the dependencies. On Windows, this may include the MSDN, too which tells you what libraries you need to link against for certain symbols you or the library may make use of.
  • The linker will never complain about includes it cannot find. Because the linker just links object files or library files together. But the linker may complain about unresolved symbols which you need to provide. So if that happens, either your setup is missing a lib, or the order is wrong.

Again, this is valid for all third party stuff you want to use. Its heavily platform and compiler dependent. The IDE should be less of concern for you. Every IDE can be setup in a way it will compile and link your stuff unless you provide everything needed as explained above.

If you don't understand parts written here it is strongly recommended you start with a book about general programming in C/C++ that covers library handling in more detail.

For the example wxChart in the end is not easy for starters. Usually you need to compile wxWidgets before, then wxChart and usually not all dependencies are explained in the docs and it behaves differently on different OS'es / compilers. Also, wcChart can be compiled in many flavours - so you need to make sure the flavour matches a) your needs and b) the way you compiled wxWidgets.

Q: My simple C++ program throws up lots of errors - what is going on?

If you have a C++ program like this:

 #include <iostream>
 int main() {
   std::cout << "hello world\n";
 }

and when you compile it you get errors like this:

 fatal error: iostream: No such file or directory

then you have probably given your source file a .c extension. If you do that, the GCC compiler (and others) will probably attempt to compile the file as a C program, not as C++. You should always give your C++ source files the extension .cpp to make sure the compiler handles them correctly.

Q: I imported a MSVCToolkit project/workspace, but Code::Blocks insists on trying to use GCC. What's wrong?

A: A little documentation problem ^^;. The "default compiler" is usually GCC, so when you imported it with "the default compiler", you told it to use GCC. To fix this situation, go to "Project", "Build Options" and select VC++ Toolkit as your compiler.

Another possibility is to put the Microsoft compiler as the default one. To do this, choose Settings - Compiler, choose the Microsoft compiler in the Selected Compiler section (top of dialog box) and press the Set as default button.

From now onwards, for all new projects the Microsoft compiler will be taken by default.

Q: When compiling a wxWidgets project, I get several "variable 'vtable for xxxx' can't be auto-imported". What's wrong?

A: You need to add WXUSINGDLL in "Project->Build options->Compiler #defines" and rebuild your project (or create a new project and use the "Using wxWidgets DLL" project option which adds "-DWXUSINGDLL" to Project->Build options->Other options). Other errors with the same resolution are: 'unresolved external symbol "char const * const wxEmptyString" (?wxEmptyString@@3PBDB)' or similar. If you were using 1.0-finalbeta and were trying to build a statically linked wxWidgets project, the cause of the problem was some faulty templates. But that's fixed now.

Q: I can't compile a multithreaded app with VC Toolkit! Where are the libraries?

A: Sorry, no fix for your problem...

Your problem doesn't come from CodeBlocks. It exists, because the free VC toolkit (VCTK) doesn't provide all the libraries and tools which come with Visual C++ (VC) which isn't free, unfortunately.

Try buying a full-fledged VC++, or even better, download MinGW

The libraries that can be obtained free of charge are:

Paths:

(VCT3) Visual C++ Toolkit 2003 - C:\Program Files\Microsoft Visual C++ Toolkit 2003\lib
(PSDK) Platform SDK - C:\Program Files\Microsoft Platform SDK\Lib
(NSDK) .NET 1.1 SDK - C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\lib

C runtime libs:

LIBC.LIB 	Single-threaded, static link                                          (VCT3, NSDK)
LIBCMT.LIB 	Multithreaded, static link                                            (VCT3, NSDK)
MSVCRT.LIB 	Multithreaded, dynamic link (import library for MSVCR71.DLL)          (NSDK)
LIBCD.LIB 	Single-threaded, static link (debug)                                  (VCT3, NSDK)
LIBCMTD.LIB 	Multithreaded, static link (debug)                                    (NSDK)
MSVCRTD.LIB 	Multithreaded, dynamic link (import library for MSVCR71D.DLL) (debug) (NSDK)

C++ libs:

LIBCP.LIB 	Single-threaded, static link                                          (VCT3, PSDK)
LIBCPMT.LIB 	Multithreaded, static link                                            (VCT3)
MSVCPRT.LIB 	Multithreaded, dynamic link (import library for MSVCP71.dll)          (none)
LIBCPD.LIB 	Single-threaded, static link (debug)                                  (VCT3)
LIBCPMTD.LIB 	Multithreaded, static link (debug)                                    (none)
MSVCPRTD.LIB 	Multithreaded, dynamic link (import library for MSVCP71D.DLL) (debug) (none)

Try setting the library linker directories to:

C:\Program Files\Microsoft Visual C++ Toolkit 2003\lib
C:\Program Files\Microsoft Platform SDK\Lib
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\lib

in that order.

The ones listed as (none) above are actually present in the IA64 and AMD64 subdirectories of the PSDK lib directory. Not sure if these would work on 32-bit windows, however, they may if they are meant to work in 32-bit compatibility mode on the 64-bit processors. Worth a try. Otherwise, you can link statically to the C++ library instead of using MSVCP71.dll. If you really want to link against MSVCP71.dll you can try to create MSVCP71.LIB from the dll using lib.exe and sed. Search google for "exports.sed" for detailed steps.

See also: tclsh script to extract import .lib from (any?) DLL (MinGW)

See also: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_c_run.2d.time_libraries.asp

See also: http://sapdb.2scale.net/moin.cgi/MS_20C_2b_2b_20Toolkit

Q: I get this error when compiling: Symbol "isascii" was not found in "codeblocks.dll"

A: Make sure you didn't mix up the MSVC headers or libs with the MinGW ones.

Q: My build fails with multiple undefined reference errors?

Example:

undefined reference to `WSACleanup@8
undefined reference to `WSACleanup@0

A: Most of the time it is because the required library is not linked with your project. Go to Project->Build options...->Linker settings (tab) and add the required library or libraries.

If the error includes a line number, it is likely that this is a problem with your code. Track down down your function declarations and implementations. Ensure they all match up, are spelled correctly, and have the correct scope resolution.

VERY often you can get help by just googling for the name of the undefined reference, for this example its "WSACleanup". Usually one of the first links is the SDK documentation, like this from MSDN for WSACleanup. You'll find there a lot useful information, including what libraries you need to link against, as for the exsample: Requirements

  • Minimum supported client: Windows 2000 Professional
  • Minimum supported server: Windows 2000 Server
  • Header: Winsock2.h
  • Library: Ws2_32.lib
  • DLL: Ws2_32.dll

The header file Winsock2.h you need to include in your sources. Most likely you have done that already because otherwise you would have gotten a compiler error unable to find the function declaration. The library you need to link against, you can remove any prefix like "lib" and the file extension like ".lib", ".a" or ".so" - so just type "Ws2_32" in the linker options. Also make sure you have added the path to that library in the linker include path's options, otherwise the linker will complain that it cannot find that library you want to link against. You also know, that you should distribute Ws2_32.dll for the runtime version of you app, luckily this one usually ships with Windows anyways, so no need to do something here.

Q: My build fails in the compile/link/run step with a Permission denied error?

A: There are several possible causes for this:

  1. The output directory does not have read/write access.
    • Either change control settings on the output directory, or move the project to different location.
  2. A previous instance of the executable failed to terminate properly.
    • Open your system's equivalent of Process/Task Manager, search the list for the name of the executable Code::Blocks is trying to output, and terminate it.
    • Logging off or rebooting will achieve the same effect.
  3. The executable is open.
    • If the executable is open in a hex-editor or actively being run, close it.
  4. Security software is interfering.
    • The target file is locked while an antivirus programming is scanning it; either wait a few seconds for the antivirus scan to finish, set an exception in the antivirus settings, or (temporarily) disable the antivirus program.
    • Firewalls with very strict settings sometimes block execution; try reducing the firewall's settings or adding an exception.
    • Switching security software may have left traces behind that are interfering; hunt down the remnants of the old antivirus/firewall software and remove them.
  5. The file/library cannot be found.
  6. Code::Blocks was improperly installed.
    • Mixing binaries from a stable release and a nightly build (or even two different nightly builds) is highly likely to cause a slew of problems; reinstall Code::Blocks in an empty directory.
  7. Multiple installed compilers are interfering with each other.
    • If they are not required to keep, completely remove all but the most used compiler.
    • If several compilers are required, ensure that none of them are in the system path (this is so that Code::Blocks will be able to manage all paths).
    • Also, do not place any compilers in their default installation path (for example C:\MinGW), as some compilers are hard-coded to look for headers in a default path before searching their own respective directories.
  8. On windows 7, the service "Application Experience" is not running as explained on stackoverflow.

See also: [/index.php/topic,15047.0.html Permission denied forums discussion]

Q: My build fails to link due to multiple definition of xyz errors?

A: GCC 4.6.1 mingw target (Windows) is known to occasionally (and erroneously) report this if link-time optimization (-flto) is used.

First, of course, check that no token has been defined multiple times. If the source code is clean, and yet the errors persist, adding linker switch (Project->Build options...->Linker settings (tab))

-Wl,--allow-multiple-definition

will enable the code to link.

See also: Bug 12762

Q: How can I change the language of the compiler (gcc) output to english?

A: Codeblocks 12.11 or higher: Settings->Environment->Environment Variables. Add "LC_ALL" with value "C". ->Set Now -> Ok

Since a few releases gcc is localized. This can make difficult to find (google ;) ) solutions for specific problems. With this setting the output is again in english.

!! this can break displaying of non-aschii characters so you can also use LC_ALL = en_US.utf8 (/index.php/topic,17579.msg120613.html#msg120613)




Return to FAQ.