Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (1) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (30) javascript (3) Linux (164) Mac (19) Machine Learning (1) mySQL (47) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (45) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Friday 4 September 2020

Build C++ app in CLion for Linux (including a MySQL application)

How to build a C++ application, using MySQL and deploy on Linux.

In this case, we are using Debian 10 (also works on Slackware 14.2)

We are building an application (example can be found here, albeit slightly modified but importantly includes mysql.h). Also on GitHub here 

Now to build this from the command line, we can use;

g++ -o test.o $(mysql_config --cflags --libs) test.cpp

This is fine, but on larger applications this isn't so practical, especially when trying to edit in vim or the likes.

I wanted to do this in CLion and while I had previously created, debugged and deployed applications remotely to Linux with CLion, there never involved header files that did not exist on my development box.

Also see here for Jetbrains documentation on setting up remote mode.

1 Set up the Remote Toolchain - PREFERENCES > ADD REMOTE HOST


2 Add the remote configuration to CMake


3 Ensure the correct Build Config is selected


4 Edit the CMakeLists.txt file as follows;

cmake_minimum_required(VERSION 3.5.2)
project(checkStamping)

set(CMAKE_CXX_STANDARD 98)

add_executable(checkStamping main.cpp)
#execute_process(COMMAND mysql_config --cflags
# OUTPUT_VARIABLE MYSQL_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
#execute_process(COMMAND mysql_config --libs
# OUTPUT_VARIABLE MYSQL_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)

#target_compile_options(checkStamping PUBLIC -I/usr/include/mariadb -I/usr/include/mariadb/mysql)

#include_directories(/usr/include/mariadb/mysql/)
#target_link_libraries(checkStamping${MYSQL_LIBS})

include_directories(/usr/include/mariadb/)
target_link_libraries(checkStamping mysqlclient)

cmake_minimum_required(VERSION 3.5.2) # FOR SLACKWARE

project(MySQL_template)

set(CMAKE_CXX_STANDARD 98)

add_executable(MySQL_template main.cpp)

#execute_process(COMMAND mysql_config --cflags
#        OUTPUT_VARIABLE MYSQL_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
#execute_process(COMMAND mysql_config --libs
#        OUTPUT_VARIABLE MYSQL_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)

#target_compile_options(checkStamping PUBLIC  -I/usr/include/mariadb -I/usr/include/mariadb/mysql)

#include_directories(/usr/include/mariadb/mysql/)
#target_link_libraries(checkStamping${MYSQL_LIBS})

# NOTE 1 & 2 SEEM TO WORK ON DEBIAN
include_directories(/usr/include/mysql/) # 1 - SLACKWARE
#include_directories(/usr/include/mariadb/) # 2 - DEBIAN
target_link_libraries(MySQL_template mysqlclient)

   It's worth noting that we can do;
   target_link_libraries(LibsModule someLib)
   where someLib could be;
   -lsomething, something.a, -L somePath (either of these)
   and it can be brought together with;
   target_link_libraries(applicationName LibsModule)
   This could be a good idea as the project grows using several libraries.
 

In the above example, the app name is MySQL_template

I would ideally like to be able to replace the two entries in red with generic paths, similar to the 

$(mysql_config --cflags --libs)

from above. This is what most of the commented out code was meant to do but unfortunately did not working during testing, so meantime;

Running mysql_conf --cflags provides the include_directories path(s)

The target_link_libraries I'm completely unsure about, with exception being the first parameter being the app name.

The above builds on CLion and can be build on the Linux box with make from the make-build-debug-remote-debian (depending on what the toolchain has been named) directory and in this example, produces a checkStamping binary in the same directory.

See also here

Note, at the time, this built fine on Debian 10 as a VM but did not work on Slackware 14.2 (naturally the g++ method from above does however work).  UPDATE: Now works as intended.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.