Jegan
Let’s say you have a legacy C/C++ project that is presently being compiled with make utility. Assuming you would like to mavenize your project with no or minimal change, here is a way you can achieve this with ease.
Let’s first create a simple C++ class header as below
1 2 3 4 5 6 7 |
#include <iostream> using namespace std; class Hello { public: void sayHello(); }; |
Let’s go ahead and define the class in hello.cpp
1 2 3 4 5 |
#include "hello.h" void Hello::sayHello() { cout << "Hello Maven from Cpp app ..." << endl; } |
Let’s write the main function in main.cpp
1 2 3 4 5 6 7 |
#include "hello.h" int main ( ) { Hello obj; obj.sayHello(); return 0; } |
Now its time to create a Makefile
1 2 3 4 5 6 7 8 9 10 11 |
all: main.o hello.o g++ -o hello.exe main.o hello.o main.o: main.cpp g++ -c main.cpp hello.o: hello.cpp g++ -c hello.cpp clean: rm -rf ./hello.exe *.o |
At this point your project home folder should look as below
You should be able to compile your project as below
Now let’s assume this is our legacy project which already works fine but you would like to build this legacy project with Maven moving forward.
Let’s create the pom.xml file in the project home as shown below
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 |
<project> <modelVersion>4.0.0</modelVersion> <groupId>org.tektutor</groupId> <artifactId>tektutor-cpp-app</artifactId> <version>1.2.3</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <executions> <execution> <id>default-compile</id> <phase>none</phase> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin<artifactId> <version>1.5.0</version> <executions> <execution> <phase>compile</phase> <goals> <goal>exec</goal> </goals> <configuration> <workingDirectory>source</workingDirectory> <executable>make</executable> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
With the above pom.xml in place, you should be able to compile the C++ project as below