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.
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, Main.java and Hoge.java. Compile
javac Main.java
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 Hoge.java 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.
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: libopencv_core.so, 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. http://exlight.net/devel/windows/dll/windll.html
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 Main.java
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. https://stackoverflow.com/questions/28727766/how-to-add-an-external-library-opencv-jar-file-to-the-java-build-path-from-th
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 Main.java
java -cp .;C:/opencv/build/java/opencv-420.jar -Djava.library.path=C:/opencv/build/java/x64 Main
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)
project(Main)
add_executable(Main main.cpp)
target_include_directories(Main
PRIVATE
/usr/local/include/opencv4
)
target_link_libraries(Main
PRIVATE
/usr/local/lib/libopencv_core.so
)
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 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