C++ static library codeblocks

I've decided to split my project that I was working on into smaller libraries, each of which will link into the final project. However when I open codeblocks and create a static library project, it creates a main.c file, not main.cpp and when I try to compile it, it tells me that it is compiling as C, not C++. Is there any reason for this and how do I change it? Am I missing something fundamental?
You can change the .c to a .cpp

Here's my library .cbp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="ULTI" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/ULTI" prefix_auto="1" extension_auto="1" />
				<Option working_dir="" />
				<Option object_output="obj/Debug/" />
				<Option type="2" />
				<Option compiler="gcc" />
				<Option createDefFile="1" />
				<Compiler>
					<Add option="-Wall" />
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/ULTI" prefix_auto="1" extension_auto="1" />
				<Option working_dir="" />
				<Option object_output="obj/Release/" />
				<Option type="2" />
				<Option compiler="gcc" />
				<Option createDefFile="1" />
				<Compiler>
					<Add option="-Wall" />
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add directory="../UTF8" />
			<Add directory="../../Libraries/curl-7.34.0-devel-mingw32/include" />
		</Compiler>
		<Linker>
			<Add library="../ULTI/libs/libadvapi32.a" />
			<Add library="../ULTI/libs/libwldap32.a" />
			<Add library="../ULTI/libs/libws2_32.a" />
			<Add library="libwinmm" />
			<Add library="libgdi32" />
			<Add library="../../MinGW/lib/libnetapi32.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/libcrypto.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/libcurl.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/libidn.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/librtmp.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/libssh2.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/libssl.a" />
			<Add library="../../Libraries/curl-7.34.0-devel-mingw32/lib/libz.a" />
		</Linker>
		<Unit filename="../ULTI/ULTI_Curl.cpp" />
		<Unit filename="../ULTI/ULTI_Curl.h" />
		<Unit filename="../ULTI/ULTI_Event.cpp" />
		<Unit filename="../ULTI/ULTI_Event.h" />
		<Unit filename="../ULTI/ULTI_File.cpp" />
		<Unit filename="../ULTI/ULTI_File.h" />
		<Unit filename="../ULTI/ULTI_Graphics.cpp" />
		<Unit filename="../ULTI/ULTI_Graphics.h" />
		<Unit filename="../ULTI/ULTI_Gui.cpp" />
		<Unit filename="../ULTI/ULTI_Gui.h" />
		<Unit filename="../ULTI/ULTI_Network.cpp" />
		<Unit filename="../ULTI/ULTI_Network.h" />
		<Unit filename="../ULTI/ULTI_Object.cpp" />
		<Unit filename="../ULTI/ULTI_Object.h" />
		<Unit filename="../ULTI/ULTI_Pack.cpp" />
		<Unit filename="../ULTI/ULTI_Pack.h" />
		<Unit filename="../ULTI/ULTI_Random.cpp" />
		<Unit filename="../ULTI/ULTI_Random.h" />
		<Unit filename="../ULTI/ULTI_Registry.cpp" />
		<Unit filename="../ULTI/ULTI_Registry.h" />
		<Unit filename="../ULTI/ULTI_Structs.cpp" />
		<Unit filename="../ULTI/ULTI_Structs.h" />
		<Unit filename="../ULTI/ULTI_Text.cpp" />
		<Unit filename="../ULTI/ULTI_Text.h" />
		<Unit filename="../ULTI/ULTI_Thread.cpp" />
		<Unit filename="../ULTI/ULTI_Thread.h" />
		<Unit filename="../ULTI/ULTI_Types.cpp" />
		<Unit filename="../ULTI/ULTI_Types.h" />
		<Unit filename="../ULTI/ULTI_Utility.cpp" />
		<Unit filename="../ULTI/ULTI_Utility.h" />
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


and my main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "../../libs/ULTI/ULTI_Network.h"
#include "../../libs/ULTI/ULTI_Event.h"
#include "../../libs/ULTI/ULTI_File.h"
#include "../../libs/ULTI/ULTI_Object.h"
#include "../../libs/ULTI/ULTI_Pack.h"
#include "../../libs/ULTI/ULTI_Random.h"
#include "../../libs/ULTI/ULTI_Registry.h"
#include "../../libs/ULTI/ULTI_Structs.h"
#include "../../libs/ULTI/ULTI_Thread.h"
#include "../../libs/ULTI/ULTI_Types.h"
#include "../../libs/ULTI/ULTI_Utility.h" 


Looks like my main is out of date, but the library includes all the necessary files through the units in the cbp anyways- I am not sure the includes are necessary
Last edited on
Ah, thank you, I didn't realise that something as simple as replacing the file would solve it. I thought that codeblocks has been set up to compile the project in C, rather than it just compiling the file in C because it's a .c file.
Definitely, and good call moving to static libraries, it sped up my build time by quite a large margin on one of my more monolithic projects.
@wizebin

Just a small but valuable thing, you should compile with at least -Wextra -pedantic-errors as well. The idea is to set the warning levels to their highest. If one was to be super pedantic, one could use -Werror to turn all warnings into errors.

Not sure if you knew this already, it just wasn't in your file :+)
@TheIdeasMan

You live up to your name perfectly! That's a great idea- I have had several syntax errors (especially not returning anything in functions- too hasty sometimes) that would have made my life quite a bit easier, sometimes you know you should do something but you forget- Thanks!
No worries :+)

Actually it's worth reading the compiler manual, there are other options that are still not turned on with those options. Here are some of them:

gcc5_3manual wrote:
-Wswitch-default
Warn whenever a switch statement does not have a default case.
-Wswitch-enum
Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. case labels outside the enumeration range also provoke warnings when this option is used. The only difference between -Wswitch and this option is that this option gives a warning about an omitted enumeration code even if there is a default label.

-Wunused
All the above -Wunused options combined.

In order to get a warning about an unused function parameter, you must either specify -Wextra -Wunused (note that -Wall implies -Wunused), or separately specify -Wunused-parameter.

-Wuninitialized
Warn if an automatic variable is used without first being initialized or if a variable may be clobbered by a setjmp call. In C++, warn if a non-static reference or non-static const member appears in a class without constructors.

If you want to warn about code that uses the uninitialized value of the variable in its own initializer, use the -Winit-self option.

These warnings occur for individual uninitialized or clobbered elements of structure, union or array variables as well as for variables that are uninitialized or clobbered as a whole. They do not occur for variables or elements declared volatile. Because these warnings depend on optimization, the exact variables or elements for which there are warnings depends on the precise optimization options and version of GCC used.

Note that there may be no warning about a variable that is used only to compute a value that itself is never used, because such computations may be deleted by data flow analysis before the warnings are printed.


Edit, some more:

-Wfloat-equal
Warn if floating-point values are used in equality comparisons.

The idea behind this is that sometimes it is convenient (for the programmer) to consider floating-point values as approximations to infinitely precise real numbers. If you are doing this, then you need to compute (by analyzing the code, or in some other way) the maximum or likely maximum error that the computation introduces, and allow for it when performing comparisons (and when producing output, but that's a different problem). In particular, instead of testing for equality, you should check to see whether the two values have ranges that overlap; and this is done with the relational operators, so equality comparisons are probably mistaken.

-Wconversion
Warn for implicit conversions that may alter a value. This includes conversions between real and integer, like abs (x) when x is double; conversions between signed and unsigned, like unsigned ui = -1; and conversions to smaller types, like sqrtf (M_PI). Do not warn for explicit casts like abs ((int) x) and ui = (unsigned) -1, or if the value is not changed by the conversion like in abs (2.0). Warnings about conversions between signed and unsigned integers can be disabled by using -Wno-sign-conversion.

For C++, also warn for confusing overload resolution for user-defined conversions; and conversions that never use a type conversion operator: conversions to void, the same type, a base class or a reference to them. Warnings about conversions between signed and unsigned integers are disabled by default in C++ unless -Wsign-conversion is explicitly enabled.
-Wzero-as-null-pointer-constant (C++ and Objective-C++ only)
Warn when a literal '0' is used as null pointer constant. This can be useful to facilitate the conversion to nullptr in C++11.
-Wfloat-conversion
Warn for implicit conversions that reduce the precision of a real value. This includes conversions from real to integer, and from higher precision real to lower precision real values. This option is also enabled by -Wconversion.
Last edited on
Topic archived. No new replies allowed.