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
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>9.Using MySQL++ in Your Own Project</title><link rel="stylesheet" type="text/css" href="tangentsoft.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="index.html" title="MySQL++ v3.2.5 User Manual"><link rel="up" href="index.html" title="MySQL++ v3.2.5 User Manual"><link rel="prev" href="configuration.html" title="8.Configuring MySQL++"><link rel="next" href="breakages.html" title="10.Incompatible Library Changes"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">9.Using MySQL++ in Your Own Project</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="configuration.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="breakages.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="incorporating"></a>9.Using MySQL++ in Your Own Project</h2></div></div></div><p>Up to now, this manual has only discussed MySQL++
in conjunction with the example programs that come with the
library. This chapter covers the steps you need to take to
incorporate MySQL++ into your own projects.</p><p>The first thing you have to do is include
<code class="filename">mysql++.h</code> in each module
that uses MySQL++. In modules that use <a class="link" href="ssqls.html" title="5.Specialized SQL Structures">SSQLS v1</a>, you also need to include
<code class="filename">ssqls.h</code>.<a href="#ftn.idp140589776950648" class="footnote" name="idp140589776950648"><sup class="footnote">[23]</sup></a></p><p>At this point, your project probably still won’t compile,
and it certainly won’t link. The remaining steps are dependent
on the operating system and tools you are using. The rest of this
chapter is broken up into several sections, one for each major
platform type. You can skip over the sections for platforms you
don’t use.</p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="inc-vstudio"></a>9.1.Visual C++</h3></div></div></div><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="inc-vstudio-mfc"></a>Using MySQL++ in an MFC Project</h4></div></div></div><p>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.</p><p>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.)</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Append the following to C/C++ | General |
Additional Include Directories: <code class="filename">C:\Program
Files\MySQL\MySQL Connector C 6.1\include,
C:\mysql++\include</code></p></li><li class="listitem"><p>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)”.</p></li><li class="listitem"><p>For both Release and Debug builds, append the following
to Linker | General | Additional Library Directories:
<code class="filename">C:\Program Files\MySQL\MySQL Connector C 6.1\lib,
C:\mysql++\lib</code></p><p>Connector/C does include debug libraries, but you
will probably not need to use them.</p></li><li class="listitem"><p>Under Linker | Input add the following to
“Additional Dependencies” for the Debug
configuration: <code class="filename">libmysql.lib wsock32.lib
mysqlpp_d.lib</code></p><p>...and then for the Release configuration:
<code class="filename">libmysql.lib wsock32.lib
mysqlpp.lib</code></p><p>This difference is because MySQL++’s Debug
DLL and import library have a <code class="filename">_d</code>
suffix so you can have both in the same directory without
conflicts.</p></li></ul></div><p>You may want to study
<code class="filename">examples\vstudio\mfc\mfc.vcproj</code>
to see this in action. Note that some of the paths will
be different, because it can use relative paths for
<code class="filename">mysqlpp.dll</code>.</p></div><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="inc-vstudio-wforms"></a>Using MySQL++ in a Windows Forms C++/CLI Project</h4></div></div></div><p>Before you start work on getting MySQL++ working with your
own program, you need to make some changes to the MySQL++ build
settings. Open <code class="filename">mysqlpp.sln</code>, then right-click
on the mysqlpp target and select Properties. Make the following
changes for both the Debug and Release configurations:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Under Configuration Properties | General,
change “Common Language Runtime support” to the
/clr setting.</p></li><li class="listitem"><p>Under C/C++ | Code Generation, change
“Enable C++ Exceptions” from “Yes
(/EHsc)” to “Yes With SEH Exceptions
(/EHa)”</p></li></ul></div><p>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
(<code class="filename">install.hta</code>) offers the option to install
the CLR version into a separate directory; use it if you need
both managed and unmanaged versions installed!</p><p>For the same reason, you might give some thought about
where you install <code class="filename">mysqlpp.dll</code> on your
end user’s machines when distributing your program.
My recommendation is to install it in the same directory as
the <code class="filename">.exe</code> file that uses it, rather than
installing into a system directory where it could conflict
with a <code class="filename">mysqlpp.dll</code> built with different
settings.</p><p>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.</p><p>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:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>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++.)</p></li><li class="listitem"><p>For the Linker | Input settings, you
don’t need <code class="filename">wsock32.lib</code>. The mere
fact that you’re using .NET takes care of that dependency
for you.</p></li></ul></div><p>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 <a class="ulink" href="http://msdn.microsoft.com/en-us/library/k8d11d4s.aspx" target="_top">the
MSDN article on the /clr switch</a> for details.</p><p>You may want to study
<code class="filename">examples\vstudio\wforms\wforms.vcproj</code>
to see all this in action. Note that some of the
paths will be different, because it can use relative
paths for <code class="filename">mysqlpp_d.dll</code> and
<code class="filename">mysqlpp.dll</code>.</p></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="inc-unix"></a>9.2.Unixy Platforms: Linux, *BSD, OS X, Cygwin, Solaris...</h3></div></div></div><p>There are lots of ways to build programs on Unixy
platforms. We’ll cover just the most generic way
here, <code class="filename">Makefile</code>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.</p><p>“Hello, world!” for MySQL++ might look something
like this:</p><pre class="programlisting">#include <mysql++.h>
int main()
{
mysqlpp::String greeting("Hello, world!");
std::cout << greeting << std::endl;
return 0;
}
</pre><p>Here’s a <code class="filename">Makefile</code> for building
that program:</p><pre class="programlisting">CXXFLAGS := -I/usr/include/mysql -I/usr/local/include/mysql++
LDFLAGS := -L/usr/local/lib
LDLIBS := -lmysqlpp -lmysqlclient
EXECUTABLE := hello
all: $(EXECUTABLE)
clean:
rm -f $(EXECUTABLE) *.o
</pre><p>The <code class="varname">*FLAGS</code> 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.</p><p>The trickiest line is the <code class="varname">LDLIBS</code>
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<a href="#ftn.idp140589776921896" class="footnote" name="idp140589776921896"><sup class="footnote">[24]</sup></a>
If you’re building a threaded program, use
<code class="filename">-lmysqlclient_r</code> instead of
<code class="filename">-lmysqlclient</code> here. (See <a class="xref" href="threads.html" title="7.Using MySQL++ in a Multithreaded Program">Section7, “Using MySQL++ in a Multithreaded Program”</a> for more details on building thread-aware
programs.)</p><p>On some systems, the order of libraries in the
<code class="varname">LDLIBS</code> 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.</p><p>You might need to add more libraries to the
<code class="varname">LDLIBS</code> line. <code class="filename">-lnsl</code>,
<code class="filename">-lz</code> and <code class="filename">-lm</code> are
common. If you study how MySQL++ itself gets built on your system,
you can see what it uses, and emulate that.</p><p>You may be wondering why we have used both
<code class="varname">LDLIBS</code> and <code class="varname">LDFLAGS</code> here. Some
<code class="filename">Makefiles</code> you have seen collect both types
of flags in a single variable. That can work if the variable
is used in the right place in the link command. However, this
particular <code class="filename">Makefile</code> is made with GNU make
in mind, and uses its standard rules implicitly. Those rules
are designed to use these two variables separately like this.
If you were writing your own compilation rules, you could write
them in such a way that you didn’t have to do this.</p><p>Beyond that, we have a pretty vanilla
<code class="filename">Makefile</code>, thanks in large part to the fact
that the default <code class="filename">make</code> rules are fine for
such a simple program.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="inc-osx"></a>9.3.OS X</h3></div></div></div><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="inc-osx-makefile"></a>Makefiles</h4></div></div></div><p>The <a class="link" href="incorporating.html#inc-unix" title="9.2.Unixy Platforms: Linux, *BSD, OS X, Cygwin, Solaris...">generic
<code class="filename">Makefile</code> instructions above</a>
cover most of what you need to know about using Makefiles on
OS X.</p><p>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 <code class="filename">ld</code> directly.</p><p>Another tricky bit on OS X is the concept of Universal
binaries. See <code class="filename">README-Mac-OS-X.txt</code> 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.</p></div><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="inc-osx-xcode"></a>Xcode</h4></div></div></div><p>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.</p></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="inc-mingw"></a>9.4.MinGW</h3></div></div></div><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="inc-mingw-makefile"></a>Makefiles</h4></div></div></div><p>The <a class="link" href="incorporating.html#inc-unix" title="9.2.Unixy Platforms: Linux, *BSD, OS X, Cygwin, Solaris...">generic
<code class="filename">Makefile</code> instructions above</a> apply
to MinGW’s version of GNU <code class="filename">make</code> as
well. You will have some differences due to the platform, so
here’s the adjusted <code class="filename">Makefile</code>:</p><pre class="programlisting">SHELL := $(COMSPEC)
MYSQL_DIR := "c:/Program Files/MySQL/MySQL Connector C 6.1"
CXXFLAGS := -I$(MYSQL_DIR)/include -Ic:/MySQL++/include
LDFLAGS := -L$(MYSQL_DIR)/lib -Lc:/MySQL++/lib/MinGW
LDLIBS := -lmysql -lmysqlpp
EXECUTABLE := hello
all: $(EXECUTABLE)
clean:
del $(EXECUTABLE)
</pre><p>Note that I’ve used <span class="command"><strong>del</strong></span>
instead of <span class="command"><strong>rm</strong></span> in the clean target. In
the past, at least, MinGW <code class="filename">make</code>
had some funny rules about whether commands in target
rules would get run with <code class="filename">sh.exe</code>
or with <code class="filename">cmd.exe</code>. I can’t
currently get my installation of MinGW to do anything
but use <code class="filename">sh.exe</code> by default, but that
may be because I have Cygwin installed, which provides
<code class="filename">sh.exe</code>. This explains the first
line in the file, which overrides the default shell with
<code class="filename">cmd.exe</code>, purely to get consistent
behavior across platforms. If you knew all your platforms
would have a better shell, you’d probably want to use
that instead.</p><p>Note the use of forward slashes in the path to the MySQL
Connector/C development files. GNU <code class="filename">make</code>
uses the backslash as an escape character, so you’d
have to double them if you’re unwilling to use forward
slashes.</p></div><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="inc-mingw-ide"></a>Third-Party MinGW IDEs (Dev-C++, Code::Blocks...)</h4></div></div></div><p>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
<code class="filename">Makefile</code> discussion above should give you
some hints.</p></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="inc-eclipse"></a>9.5.Eclipse</h3></div></div></div><p>As far as I can tell, the simplest way to build a C++ project
with Eclipse is to set up a <code class="filename">Makefile</code> for it
as described <a class="link" href="incorporating.html#inc-unix" title="9.2.Unixy Platforms: Linux, *BSD, OS X, Cygwin, Solaris...">above</a>, then add an
external run configuration for your local <code class="filename">make</code>
tool. Get the project building from the command line with
<code class="filename">make</code>, then go to Run | External Tools | Open
External Tools Dialog and add a new launch configuration.</p><p>For example, on my OS X system I use
<code class="filename">/usr/bin/gnumake</code> for the program location
and pick the project root with the Browse Workspace button to
set the working directory.</p></div><div class="footnotes"><br><hr style="width:100; text-align:left;margin-left: 0"><div id="ftn.idp140589776950648" class="footnote"><p><a href="#idp140589776950648" class="para"><sup class="para">[23] </sup></a>MySQL++ has many header
files, but the only one that isn’t intertwined with the rest is
<code class="filename">ssqls.h</code>. <code class="filename">mysql++.h</code> brings
in all of the others in the correct order. Some have tried to speed
their build times by finding a subset of MySQL++ headers to include,
but <code class="filename">mysql++.h</code> already does as much of this as
is practical. MySQL++’s monolithic nature rules out finding
a true subset of the library headers.</p></div><div id="ftn.idp140589776921896" class="footnote"><p><a href="#idp140589776921896" class="para"><sup class="para">[24] </sup></a>The MySQL C API library
is most commonly called <code class="filename">libmysqlclient</code>
on Unixy systems, though it is also known as <a class="ulink" href="https://dev.mysql.com/downloads/connector/c/" target="_top">Connector/C</a>.</p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="configuration.html">Prev</a></td><td width="20%" align="center"></td><td width="40%" align="right"><a accesskey="n" href="breakages.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.Configuring MySQL++</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">10.Incompatible Library Changes</td></tr></table></div></body></html>
|