Home » How to install Xerces-C++ from source on Ubuntu PC?

How to install Xerces-C++ from source on Ubuntu PC?

Remark: This tutorial describes how to install Apache Xerces-C++ 3.2.2 from source on a Ubuntu 18.04 LTS PC, but it can be easily adapted for other versions of Xerces and/or Linux distributions.

Step 1: Create a new script

All required steps to install Xerces-C++ will be written in a script called XercesCppSetup.sh (assuming you do not have a file with that name in the current folder already).

To create the file execute the following on the command line (excluding the “$” symbol):
$ touch XercesCppSetup.sh

Step 2: Install dependencies and Xerces-C++

Open the script using your favourite text editor. Assuming this is vi(m) execute the following on the command line:
$ vim XercesCppSetup.sh

Next write the contents of the script (i.e. copy and paste the text below into the script):

/scripts/blob/master/ubuntu/setup/XercesCppSetup.sh
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
#!/usr/bin/env bash
 
 
# =============================================================================
# Constants
# =============================================================================
 
CPUS=$(nproc --all);
CURRENT_DIR_PATH="$(pwd)";
XERCES_CPP_INSTALL_DIR_PATH="/opt/xerces-c";
XERCES_CPP_LIB_NAME="xerces-c-3.2.2";
XERCES_CPP_LIB_DIR_PATH="${CURRENT_DIR_PATH}/${XERCES_CPP_LIB_NAME}";
XERCES_CPP_LIB_ARCHIVE_PATH="${CURRENT_DIR_PATH}/${XERCES_CPP_LIB_NAME}.tar.gz";
XERCES_CPP_LIB_ARCHIVE_URL="http://mirror.ox.ac.uk/sites/rsync.apache.org/xerces/c/3/sources/${XERCES_CPP_LIB_NAME}.tar.gz";
USERNAME=$(whoami);
 
XERCES_CPP_BUILD_DIR_PATH="${XERCES_CPP_LIB_DIR_PATH}/build";
 
# If the current user is not "root" then there is a need for an explicit sudo
# command
if [[ $(id -u) -ne 0 ]]; then
    SUDO_CMD="sudo";
fi
 
 
# =============================================================================
# Functions
# =============================================================================
 
# Print the given message to the standard output.
#
# The given message will be prefixed by the current time when printed to the
# standard output.
#
# Arguments:
#   $1 - A string variable recording the given message.
printMessage() {
    local currentTime;
 
    currentTime=$(date +"%Y-%m-%d %H:%M:%S");
 
    echo -e "${currentTime}\\t${1}";
}
 
# Check the return code of the most recently executed command.
#
# If the return code is non-zero then exit with the given error message.
#
# Arguments:
#   $1 - A string variable recording the given error message.
checkReturnCode() {
    if [[ $? -ne 0 ]]; then
        printMessage "$1";
 
        exit 1;
    fi
}
 
 
# =============================================================================
# Main
# =============================================================================
 
printMessage "Setting up Xerces-C++ from source...";
printMessage "CPU(s): ${CPUS}";
printMessage "User: ${USERNAME}";
 
# Install dependencies
printMessage "Installing dependencies...";
 
${SUDO_CMD} apt update &&   \
${SUDO_CMD} apt install -y  \
    cmake                   \
    g++-multilib            \
    make                    \
    wget;
checkReturnCode "Failed to install dependencies.";
 
printMessage "Successfully installed dependencies.";
 
# Download Xerces-C++ archive
printMessage "Downloading Xerces-C++ archive from ${XERCES_CPP_LIB_ARCHIVE_URL} to ${XERCES_CPP_LIB_ARCHIVE_PATH}...";
 
wget -O "${XERCES_CPP_LIB_ARCHIVE_PATH}" "${XERCES_CPP_LIB_ARCHIVE_URL}";
checkReturnCode "Failed to download Xerces-C++ archive.";
 
printMessage "Successfully downloaded Xerces-C++ archive.";
 
# Extract Xerces-C++ archive
printMessage "Extracting and then removing Xerces-C++ archive ${XERCES_CPP_LIB_ARCHIVE_PATH}...";
 
tar xvzf "${XERCES_CPP_LIB_ARCHIVE_PATH}" && \
rm -f "${XERCES_CPP_LIB_ARCHIVE_PATH}";
checkReturnCode "Failed to extract and then remove Xerces-C++ archive.";
 
printMessage "Successfully extracted and then removed Xerces-C++ archive.";
 
# Configure Xerces-C++
printMessage "Configuring Xerces-C++...";
 
mkdir -p "${XERCES_CPP_BUILD_DIR_PATH}" &&                  \
cd "${XERCES_CPP_BUILD_DIR_PATH}" &&                        \
CFLAGS=-O3 CXXFLAGS=-O3 cmake                               \
    -G "Unix Makefiles"                                     \
    -DCMAKE_INSTALL_PREFIX="${XERCES_CPP_INSTALL_DIR_PATH}" \
    -DCMAKE_BUILD_TYPE=Release                              \
    "${XERCES_CPP_LIB_DIR_PATH}";
checkReturnCode "Failed to configure Xerces-C++.";
 
printMessage "Successfully configured Xerces-C++.";
 
# Build Xerces-C++
printMessage "Building Xerces-C++...";
 
make -j "${CPUS}";
checkReturnCode "Failed to build Xerces-C++.";
 
printMessage "Successfully built Xerces-C++.";
 
# Install Xerces-C++
printMessage "Installing Xerces-C++ in directory ${XERCES_CPP_INSTALL_DIR_PATH}...";
 
make -j "${CPUS}" install;
checkReturnCode "Failed to install Xerces-C++.";
 
printMessage "Successfully installed Xerces-C++.";
 
printMessage "Successfully set up Xerces-C++ from source.";

The comments in the script should provide sufficient details in order to understand what each instruction does.

Step 3: Execute script

Enable the current user to execute the script XercesCppSetup.sh by running the following on the command line:
$ chmod +x XercesCppSetup.sh

Finally execute the script as below:
$ ./XercesCppSetup.sh

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.