Organize builds in C ++ / Java and Win / Linux combinations


In this article, I will organize my knowledge about building about two programming languages, C ++ and Java. Specifically, we will organize "basic build method", "how to use shared library", and "build tool" in each language. At that time, if there is a difference between Windows and Linux, that point will also be mentioned.

The purpose of posting this article is to organize the knowledge by the poster himself, and the content may contain mistakes. If you find any mistakes, please point them out in the comments.

Basic build method

This section summarizes the basic methods of building from the command line. Described as "build = compile + link".

C++ Suppose your project consists of three files: main.cpp, hoge.cpp, and hoge.h. Compile

g++ -c main.cpp hoge.cpp

The link is

g++ main.o hoge.o

It can be done with the command, and an executable file (a.out on Linux, a.exe on Windows) is generated. -c is a "compile only" option, and if you omit this option, you can even generate an executable file at once.

g++ main.cpp hoge.cpp

Java Suppose your project consists of two files, and Compile


You can do this with the command, and the files Main.class and Hoge.class will be generated. Dependencies are resolved automatically, so you don't need to add as an argument. Also, since the link is done at runtime, there is no need to explicitly link with the command. Execution

java Main

It is done with the command.

How to use the shared library

We will explain how to use the shared library using OpenCV as an example. The only reason for using OpenCV as an example is that it is a library familiar to posters, so if you are not familiar with OpenCV, please replace it with another library and read it.

C++(Linux) Specify the include path, library path, and shared library file with g ++ options. For example, if the include path: /usr/local/include/opencv4, the library path: /usr/local/lib, and the shared library file:, do as follows.

g++ main.cpp -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_core

C++(Windows) For Windows, specify the import library (.lib file) with the -l option. If the import library file name is opencv_world420.lib, build it with the following command.

g++ main.cpp -IC:/opencv/build/include -LC:/opencv/build/x64/lib -lopencv_world420

At runtime, you need to pass the path to the .dll file (shared library itself) that is paired with the .lib file. For the relationship between .lib files and .dll files, the following sites may be helpful.

Java(Linux) In Java, the jar file plays a role like the above import library, so specify the jar file in the classpath and build as follows.

javac -cp /usr/local/share/java/opencv4/opencv-430.jar

And at runtime you need to give the path to the .so file, which is the main body of the shared library, as java.library.path. (Although I will omit the explanation, you need to load the .so file with the System.loadLibrary function also on the source code) You also need the classpath at runtime and specify the path (.) Of the class file you want to run.

java -cp .:/usr/local/share/java/opencv4/opencv-430.jar -Djava.library.path=/usr/local/share/java/opencv4 Main

For details, please refer to the following page.

Java(Windows) On Windows, load the .dll file instead of the .so file and the classpath delimiter will be ";" instead of ":".

javac -cp C:/opencv/build/java/opencv-430.jar
java -cp .;C:/opencv/build/java/opencv-420.jar -Djava.library.path=C:/opencv/build/java/x64 Main

Build tool

This section introduces CMake, a C language build tool, and Gradle, a Java build tool.

CMake Before we talk about CMake, let's talk about make. make is a pioneer of build tools, and you can automate builds by writing build rules in the Makefile.

CMake is often used in the procedure of CMake → make → make install when installing C language source code. In each of these steps, the following tasks are performed.

In the example using OpenCV, CMakeLists.txt has the following contents. (When not using pkg_config)

cmake_minimum_required(VERSION 3.10)

add_executable(Main main.cpp)



Gradle As for Gradle, I will put only the contents of build.gradle as a memo for myself.

apply plugin: 'java'

dependencies {
    compile files("/usr/local/share/java/opencv4/opencv-430.jar")

in conclusion

In this article, I have organized my knowledge about building about two programming languages, C ++ and Java. Originally I was going to touch on the build process hidden in the IDE, but I'm not sure if it's necessary anymore, so I'd like to end here. As a result, the content is similar to the article 20 years ago, but I would appreciate it if you could refer to it.

Recommended Posts

Organize builds in C ++ / Java and Win / Linux combinations
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Differences in writing Java, C # and Javascript classes
Implement math combinations in Java
Reproduce Java enum in C #
C # and Java Overrides Story
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Read Java properties file in C #
Try mixing C and Swift in one project (OS X, Linux)
Encoding and Decoding example in Java
Java, JavaScript, C # (difference in assignment)
CGI in C and Dart: Introduction (1)
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
StringBuffer and StringBuilder Class in Java
Understanding equals and hashCode in Java
Encrypt with Java and decrypt with C #
Hello world in Java and Gradle
Try mixing more C and Swift in one project (OS X, Linux)
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Difference between final and Immutable in Java
Link Java and C ++ code with SWIG
[Java] for Each and sorted in Lambda
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
Parse the date and time string formatted by the C asctime function in Java
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Regarding the transient modifier and serialization in Java
Create barcodes and QR codes in Java PDF
Detect similar videos in Java and OpenCV rev.2
Difference between next () and nextLine () in Java Scanner
Try using Sourcetrail (win version) in Java code
Capture and save from selenium installation in Java
Detect similar videos in Java and OpenCV rev.3
Add, read, and delete Excel comments in Java
Check static and public behavior in Java methods
[Java] Understand in 10 minutes! Associative array and HashMap
Basics of threads and Callable in Java [Beginner]
Summarize the differences between C # and Java writing
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Upload and download notes in java on S3
Encrypt / decrypt with AES256 in PHP and Java
Generate OffsetDateTime from Clock and LocalDateTime in Java