Using Maven for C/C++ Projects

Using Maven for C/C++ projects

I highly recommend the maven-nar-plugin. I find it superior in many ways to the alternatives. It doesn't require listing out source files, handles multiple OSes and architectures, handles unit and integration tests, and generally follows "the maven way". It introduces a new kind of packaging - the NAR, or "native archive", that contains the artifact you care about (.dll, .so, .a, .exe, etc.) but also metadata, headers, etc. in a way that makes sense.

It does require a bit of up front work to package third-party software up into NARs, but its pretty straightforward. Once they are NARs, you simply use the normal Maven dependency mechanism to link with them, for example:

<dependency>
<groupId>cppunit</groupId>
<artifactId>cppunit</artifactId>
<scope>test</scope>
</dependency>

One drawback is that it does not appear to be actively maintained, but it is full-featured and is a rather impressive example of Maven plugin authoring.

Using Maven to build c++ in eclipse

Eclipse does not use the Maven build. Instead it configures the JDT according to your POM. This is done through an M2E (Maven to Eclipse) connector. If you want the same to work for CDT, then you would need an M2E connector for that. I'm not aware of an existing one, so you would have to write an Eclipse plugin for that. See http://wiki.eclipse.org/M2E/Extension_Development for details on how to do that.

Of course, you could also use "execute" instead of "ignore" in your lifecycleMapping in the pom. But that would probably make for a pretty slow build, because the whole build would be triggered on each change. And still this wouldn't "magically" make the errors from Maven appear in your files.

Can't build C/C++ projects using maven-nar plugin

Yes, that error is caused by a command line tool being unavailable on the path. However, in this case, since your AOL is x86-Windows-msvc, the relevant command line tool is link, not gcc. (You can see this in the source code.)

Of course, you aren't trying to use the MSVC compiler at all, so you shouldn't need link in your path anyway. To use gcc instead, you need to configure that in your POM in the maven-nar-plugin configuration. Here is a sample configuration (from this project, which you will of course need to tweak to your needs):

<configuration>
<c>
<name>gcc</name>
<includes>
<include>**/*.c</include>
</includes>
<options>
<option>-I${JAVA_HOME}/include</option>
<option>${java.os.include}</option>
<option>${stack.protector.option}</option>
<option>${architecture.option}</option>
<option>${subsystem.option}</option>
<option>${debug.option}</option>
</options>
</c>
<linker>
<name>gcc</name>
<options>
<option>${architecture.option}</option>
<option>${subsystem.option}</option>
</options>
<sysLibs>
<sysLib>
<name>${libdl.name}</name>
</sysLib>
</sysLibs>
</linker>
<libraries>
<library>
<type>executable</type>
<!-- <run>true</run> -->
<subSystem>gui</subSystem>
</library>
</libraries>
</configuration>

Is the maven-native-plugin widely used to build C++ projects using maven?

In my experience, the C++ community still hasn't standardised on a common build tool. While the GNU autotools (and GNU make) are still popular for Open Source projects, other options include SCons, CMake, makepp and bjam/jam.

Personally, I would only use Maven for a project that's mainly written in Java with a small JNI part.

Building C/C++ Maven NAR project from CLion terminal using Visual C++ compiler (msvc): Cannot deduce version number

Cannot deduce version number from: ->

NAR tries to make a system call to link.exe and/or cl.exe (if I recall correctly) with a flag like /version, so that it can determine the version of MSVS, so that it can pass arguments in the correct way for that version. This is necessary because different versions of MSVS are not backwards compatible when it comes to the command line syntax.

However, if the executable in question is not found, the command spits out nothing on stdout (again, IIRC), producing an empty string for the version output, which cannot be parsed.

It would certainly be nice to improve this error message to be more clear. In the meantime, you can try running with mvn -X to invoke debug mode, which will emit a lot more output from Maven, including a bunch more from NAR.

Anyway, the crux of the issue is that the CLion CLI environment (i.e.: which variables are set to what) must differ from the CLI environment of your Developer Command Prompt. You could try using set to list variables in each, then diff the outputs.

To fix this, you will probably need to either: A) tweak something in your CLion environment; or B) teach the NAR plugin about CLion and/or your MSVS configuration by patching it.

If you go the route of (B), feel free to submit a PR! And either way, adding something about CLion to the NAR wiki would be great.



Related Topics



Leave a reply



Submit