Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing the source code base when mixing two or more languages (like Java and C++)

I ran into a problem a few days ago when I had to introduce C++ files into a Java project. It started with a need to measure the CPU usage of the Java process and it was decided that the way to go was to use JNI to call out to a native library (a shared library on a Unix machine) written in C. The problem was to find an appropriate place to put the C files in the source repository (incidentally Clearcase) which consists of only Java files.

I thought of a couple of alternatives:

(a) Create a separate directory for putting the C files (specifically, one .h file and one .c file) at the top of the source base like:

/vobs/myproduct/javasrc /vobs/myproduct/cppsrc

I didn't like this because I have only two C files and it seemed very odd to split the source base at the language level like this. Had substantial portions of the project been written more or less equally in C++ and Java, this could be okay.

(b) Put the C files into the Java package that uses it.

I have the calling Java classes in /vobs/myproduct/com/mycompany/myproduct/util/ and the C files also go in there.

I didn't like this either because I think the C files just don't belong in the Java package.

Has anybody solved a problem like this before? Generally, what's a good strategy to follow when organizing codebase that mixes two or more languages?

Update: I don't have any plan to use any C or C++ in my project, some Jython perhaps, but you never know when my customers need a feature that can be solved only by using C or best solved by using C.

like image 875
trshiv Avatar asked Sep 24 '08 12:09

trshiv


2 Answers

"I didn't like this because I have only two C files and it seemed very odd to split the source base at the language level like this"

Why does it seem odd? Consider this project:

  project1\src\java
  project1\src\cpp
  project1\src\python

Or, if you decide to split things up into modules:

  project1\module1\src\java
  project1\module1\src\cpp
  project1\module2\src\java
  project1\module2\src\python

I guess it's a matter of personal taste, but the above structure is fairly common, and I think it works quite well once you get used to it.

like image 128
Anders Sandvig Avatar answered Nov 26 '22 05:11

Anders Sandvig


The default Maven-generated layout for web apps is src/main/java, src/test/java, src/main/resources, and src/test/resources. I would assume that it would default to adding src/main/cpp and src/test/cpp as well. This seems like a decent enough convention to me.

like image 31
Jim Kiley Avatar answered Nov 26 '22 07:11

Jim Kiley