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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook V4.3//EN"
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<sect1 id="incorporating">
<title>Using MySQL++ in Your Own Project</title>
<para>Up to now, this manual has only discussed MySQL++ in
conjunction with the example programs that come with it. This chapter
covers the steps you need to take to incorporate MySQL++ into your own
projects.</para>
<para>The first thing you have to do is include
<filename>mysql++.h</filename> in each module that uses MySQL++.
In modules that use <link linkend="ssqls">SSQLS</link>, you also
need to include <filename>ssqls.h</filename>.<footnote><para>MySQL++
has many more header files, but don’t include any of them
directly. <filename>mysql++.h</filename> includes all of them for
you (except <filename>ssqls.h</filename> of course) in the correct
order.</para></footnote></para>
<para>At this point, your project probably won’t compile, and
it certainly won’t link. The steps to fix this are dependent
on your particular platform. The rest of this chapter is broken
up into several sections, one for each major platform type. These
sections are independent of each other. Feel free to ignore the
sections for platforms you don’t use.</para>
<sect2 id="inc-vstudio">
<title>Visual C++</title>
<sect3 id="inc-vstudio-mfc">
<title>Using MySQL++ in an MFC Project</title>
<para>If you don’t already have a project set up, open
Visual Studio, say File | New | Project, then choose Visual C++
| MFC | MFC Application. Go through the wizard setting up the
project as you see fit.</para>
<para>Once you have your project open, right click on your
top-level executable in the Solution Explorer, choose Properties,
and make the following changes. (Where it doesn’t
specify Debug or Release, make the same change to both
configurations.)</para>
<itemizedlist>
<listitem><para>Append the following to C/C++
| General | Additional Include Directories:
<filename>C:\Program Files\MySQL\MySQL Server 5.0\include,
C:\mysql++\include</filename></para></listitem>
<listitem><para>Under C/C++ | Code Generation change
“Runtime Library” to “Multi-threaded
Debug DLL (/MDd)” for the Debug configuration. For
the Release configuration, make it “Multi-threaded DLL
(/MD)”.</para></listitem>
<listitem>
<para>Append the following to Linker | General |
Additional Library Directories for the Debug configuration:
<filename>C:\Program Files\MySQL\MySQL Server 5.0\lib\debug,
C:\mysql++\vc\debug</filename></para>
<para>For the Release configuration, make it the same,
but change the “debug” directory names to
“opt”.</para>
</listitem>
<listitem>
<para>Under Linker | Input add the following to
“Additional Dependencies” for the Debug
configuration: <filename>libmysql.lib wsock32.lib
mysqlpp_d.lib</filename></para>
<para>...and then for the Release configuration:
<filename>libmysql.lib wsock32.lib
mysqlpp.lib</filename></para>
<para>This difference is because MySQL++’s Debug
DLL and import library have a <filename>_d</filename>
suffix so you can have both in the same directory without
conflicts.</para>
</listitem>
</itemizedlist>
<para>You may want to study
<filename>examples\vstudio\mfc\mfc.vcproj</filename>
to see this in action. Note that some of the paths will
be different, because it can use relative paths for
<filename>mysqlpp.dll</filename>.</para>
</sect3>
<sect3 id="inc-vstudio-wforms">
<title>Using MySQL++ in a Windows Forms C++/CLI Project</title>
<para>Before you start work on getting MySQL++ working with your
own program, you need to make some changes to the MySQL++ build
settings. Open <filename>mysqlpp.sln</filename>, then right-click
on the mysqlpp target and select Properties. Make the following
changes for both the Debug and Release configurations:</para>
<itemizedlist>
<listitem><para>Under Configuration Properties | General,
change “Common Language Runtime support” to the
/clr setting.</para></listitem>
<listitem><para>Under C/C++ | Code Generation, change
“Enable C++ Exceptions” from “Yes
(/EHsc)” to “Yes With SEH Exceptions
(/EHa)”</para></listitem>
</itemizedlist>
<para>If you have already built MySQL++, be sure to perform a
complete rebuild after changing these options. The compiler
will emit several C4835 warnings after making those changes,
which are harmless when using the DLL with a C++/CLI
program, but which warn of real problems when using it with
unmanaged C++. This is why MySQL++’s Windows installer
(<filename>install.hta</filename>) offers the option to install
the CLR version into a separate directory; use it if you need
both managed and unmanaged versions installed!</para>
<para>For the same reason, you might give some thought about
where you install <filename>mysqlpp.dll</filename> on your
end user’s machines when distributing your program.
My recommendation is to install it in the same directory as
the <filename>.exe</filename> file that uses it, rather than
installing into a system directory where it could conflict
with a <filename>mysqlpp.dll</filename> built with different
settings.</para>
<para>Once you have MySQL++ built with CLR support, open your
program’s project. If you don’t already have a
project set up, open Visual Studio, say File | New | Project,
then choose Visual C++ | CLR | Windows Forms Application.
Go through the wizard setting up the project as you see
fit.</para>
<para>The configuration process isn’t much different from
that for an MFC project, so go through the list above first.
Then, make the following changes particular to .NET and
C++/CLI:</para>
<itemizedlist>
<listitem><para>Under Configuration Properties | General
change the setting from /clr:pure to /clr. (You need mixed
assembly support to allow a C++/CLI program to use a plain C++
library like MySQL++.)</para></listitem>
<listitem><para>For the Linker | Input settings, you
don’t need <filename>wsock32.lib</filename>. The mere
fact that you’re using .NET takes care of that dependency
for you.</para></listitem>
</itemizedlist>
<para>In the MFC instructions above, it said that you need to
build it using the Multi-threaded DLL version of the C++ Runtime
Library. That’s not strictly true for MFC, but it’s
an absolute requirement for C++/CLI. See the Remarks in <ulink
url="http://msdn.microsoft.com/en-us/library/k8d11d4s.aspx">the
MSDN article on the /clr switch</ulink> for details.</para>
<para>You may want to study
<filename>examples\vstudio\wforms\wforms.vcproj</filename>
to see all this in action. Note that some of the
paths will be different, because it can use relative
paths for <filename>mysqlpp_d.dll</filename> and
<filename>mysqlpp.dll</filename>.</para>
</sect3>
</sect2>
<sect2 id="inc-unix">
<title>Unixy Platforms: Linux, *BSD, OS X, Cygwin, Solaris...</title>
<para>There are lots of ways to build programs on Unixy
platforms. We’ll cover just the most generic way
here, <filename>Makefile</filename>s. We’ll use a very
simple example so it’s clear how to translate this to
more sophisticated build systems such as GNU Autotools or
Bakefile.</para>
<para>“Hello, world!” for MySQL++ might look something
like this:</para>
<programlisting><xi:include href="hello.cpp" parse="text"
xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
<para>Here’s a <filename>Makefile</filename> for building
that program:</para>
<programlisting><xi:include href="Makefile.hello.posix" parse="text"
xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
<para>The first three lines are where all of the assumptions
about file and path names are laid out. Probably at least one of
these assumptions isn’t true for your system, and so will
require changing.</para>
<para>The trickiest line is the third one. MySQL++ programs
need to get built against both the MySQL and MySQL++
libraries, because MySQL++ is built on top of the MySQL C
API library. If you’re building a threaded program,
use <filename>-lmysqlclient_r</filename> instead. (See <xref
linkend="threads"/> for more details on building thread-aware
programs.)</para>
<para>On some systems, the order of libraries in the
<varname>LDFLAGS</varname> line is important: these linkers collect
symbols from right to left, so the rightmost library needs to
be the most generic. In this example, MySQL++ depends on MySQL,
so the MySQL C API library is rightmost.</para>
<para>You might need to add more libraries to the
<varname>LDFLAGS</varname> line. <filename>-lnsl</filename>,
<filename>-lz</filename> and <filename>-lm</filename> are
common. If you study how MySQL++ itself gets built on your system,
you can see what it uses, and emulate that.</para>
<para>Beyond that, we have a pretty vanilla
<filename>Makefile</filename>. We don’t have any special
dependency or build rules, because the default rules should work
fine, particularly if you’re using GNU make, which is just
about universal these days.</para>
</sect2>
<sect2 id="inc-osx">
<title>OS X</title>
<sect3 id="inc-osx-makefile">
<title>Makefiles</title>
<para>The <link linkend="inc-unix">generic Makefile
instructions above</link> cover most of what you need to know
about using Makefiles on OS X.</para>
<para>One thing that may trip you up on OS X is that it uses an
uncommon dynamic linkage system. The easiest way to cope with
this is to link your executables with the compiler, rather than
call <filename>ld</filename> directly.</para>
<para>Another tricky bit on OS X is the concept of Universal
binaries. See <filename>README-Mac-OS-X.txt</filename> for
details on building a Universal version of the MySQL++ library,
if you need one. By default, you only get a version tuned for
the system type you build it on.</para>
</sect3>
<sect3 id="inc-osx-xcode">
<title>Xcode</title>
<para>I have no information on how to incorporate MySQL++ in
an Xcode project. Send a message to the MySQL++ mailing list
if you can help out here.</para>
</sect3>
</sect2>
<sect2 id="inc-mingw">
<title>MinGW</title>
<sect3 id="inc-mingw-makefile">
<title>Makefiles</title>
<para>The <link linkend="inc-unix">generic Makefile instructions
above</link> apply to MinGW’s version of GNU make as
well. You will have some differences due to the platform, so
here’s the adjusted <filename>Makefile</filename>:</para>
<programlisting><xi:include
href="Makefile.hello.mingw" parse="text"
xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
<para>Note the use of forward slashes. Also, we use
<command>del</command> instead of <command>rm</command>
in the clean target; this assumes there is no
<filename>sh.exe</filename> in your <varname>PATH</varname>,
which may not be true if you have Cygwin or MSYS installed. Read
on to see how to cope with that.</para>
<sect4 id="inc-mingw-cygwin">
<title>Working with MinGW Under Cygwin</title>
<para>Compared to Unix, the biggest difference you’ll
find is that MinGW calls its <filename>make</filename>
executable <filename>mingw32-make</filename>. As I understand
it, this is to allow it to coexist with Cygwin, since the two
versions have some behavioral differences, despite both being
based on GNU Make. A <filename>Makefile</filename> written
for one is likely to fail to work correctly with the other,
so you have to be able to specify which one you mean.</para>
<para>If you have both MinGW and Cygwin installed, you may be
tempted to use Cygwin’s superior command line environment
over a Windows command shell or MSYS. If you’re like me,
you type <filename>make</filename> reflexively now; typing
<filename>mingw32-make</filename> instead isn’t going
to work. Another problem with having Cygwin and MinGW on the
same system is that this puts a <filename>sh.exe</filename>
program in your system’s <varname>PATH</varname>
which makes MinGW make send shell commands to it instead of
<filename>cmd.exe</filename> as it normally would. I find it
best to set up a special MinGW environment to avoid problems
stemming from these platform differences.</para>
<para>I’ve created a pair of scripts that let me work
in Cygwin mode most of the time and temporarily drop down
into “MinGW mode” only when necessary. I call the
first script <filename>mingw</filename>, and put it somewhere
in the Cygwin <varname>PATH</varname>:</para>
<programlisting>
#!/bin/sh
PATH=/cygdrive/c/mingw/bin:/cygdrive/c/windows:/cygdrive/c/windows/system32:/cygdrive/c/cygwin/bin
echo "Say 'exit' to leave MinGW shell and restore Cygwin environment."
/usr/bin/bash --rcfile ~/.mingwrc
</programlisting>
<para>Then there’s a tiny little file called
<filename>.mingwrc</filename> that goes in your Cygwin home
directory:</para>
<programlisting>
alias make=mingw32-make
PS1='MinGW: \W \$ '
</programlisting>
<para>(This split is necessary due to the way Bash works.)</para>
<para>The first script sets up most of the MinGW environment,
putting the MinGW and Windows directories ahead of the Cygwin
directory so programs in those locations take precedence. Then
the second script finishes setting up the MinGW sub-shell,
causing the <filename>make</filename> command to invoke
MinGW’s make program instead of Cygwin’s, and
changing the command prompt to remind you that you’re
in a sub-shell. Just say <command>exit</command> to get back
to Cygwin mode.</para>
</sect4>
</sect3>
<sect3 id="inc-mingw-ide">
<title>Third-Party MinGW IDEs (Dev-C++, Code::Blocks...)</title>
<para>I have no information on how to do this. We’ve
received reports on the mailing list from people that have made
it work, but no specifics on what all needs to be done. The
Makefile discussion above should give you some hints.</para>
</sect3>
</sect2>
<sect2 id="inc-eclipse">
<title>Eclipse</title>
<para>As far as I can tell, the simplest way to build a C++
project with Eclipse is to set up a Makefile for it as described
<link linkend="inc-unix">above</link>, then add an external
run configuration for your local make tool. Get the project
building from the command line with make, then go to Run |
External Tools | Open External Tools Dialog and add a new launch
configuration.</para>
<para>For example, on my OS X system I use
<filename>/usr/bin/gnumake</filename> for the program location
and pick the project root with the Browse Workspace button to
set the working directory.</para>
</sect2>
</sect1>
|