preprocessor idea

P

Philipp Kraus

Hello,

I use within my Java code different external libraries (DLL), so I have
create a string array with their names
so the libraries can be load with System.load / System.loadLibrary.

I build the libraries (C++ code) with a toolchain and I use a lot of
preprocessor commands within
the C++ code, so I can enable or disable some function and so the using
of the libraries eg:

c++:
#ifdef lib1
#endif

#ifdef lib2
#endif

So my Java code must load lib1 and lib2 if both defs are set. Java does
not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java code
during the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"}). Do you know any
idea for this problem?

Thanks

Phil
 
R

Robert Klemme

I use within my Java code different external libraries (DLL), so I have
create a string array with their names
so the libraries can be load with System.load / System.loadLibrary.

That's the wrong approach IMHO: make the interface classes load the DDL
they need at class load time (e.g. in a static initializer block). This
best fits the Java model of dynamically deciding what code to use.
I build the libraries (C++ code) with a toolchain and I use a lot of
preprocessor commands within
the C++ code, so I can enable or disable some function and so the using
of the libraries eg: >
Thanks

Phil


Why do you want to decide this in C++ land?
c++:
#ifdef lib1
#endif

#ifdef lib2
#endif

So my Java code must load lib1 and lib2 if both defs are set. Java does
not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java code
during the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"}). Do you know any
idea for this problem?

Again, IMHO this is the wrong approach: you should create your libraries
with all the functionality you need and only determine via Java classes
which are used or not used what DLLs are loaded and used.

If you really want to make the decision in C++ land you could use your
preprocessor to have different variants of a JNI method which returns
information which lets you determine in Java which DLL's to load. You
could have a native method which returns a String[] with DLL names etc.

Kind regards

robert
 
M

markspace

a JNI method which returns
information which lets you determine in Java which DLL's to load. You
could have a native method which returns a String[] with DLL names etc.


+1. I could cite some design patterns or whatnot why this is probably
the best approach but "common sense" is I think the most applicable
design pattern here. Why go around your elbow to get from your
forefinger to your thumb?
 
A

Arne Vajhøj

I use within my Java code different external libraries (DLL), so I have
create a string array with their names
so the libraries can be load with System.load / System.loadLibrary.

I build the libraries (C++ code) with a toolchain and I use a lot of
preprocessor commands within
the C++ code, so I can enable or disable some function and so the using
of the libraries eg:

c++:
#ifdef lib1
#endif

#ifdef lib2
#endif

So my Java code must load lib1 and lib2 if both defs are set. Java does
not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java code
during the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"}). Do you know any
idea for this problem?

Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

Arne
 
R

Roedy Green

I use within my Java code different external libraries (DLL), so I have
create a string array with their names
so the libraries can be load with System.load / System.loadLibrary.

Recall that if ( DEBUGGING ) { out. println( "I'm here"); }
generates no code if the static final boolean DEBUGGING is false.
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
R

Robert Klemme

I use within my Java code different external libraries (DLL), so I have
create a string array with their names
so the libraries can be load with System.load / System.loadLibrary.

I build the libraries (C++ code) with a toolchain and I use a lot of
preprocessor commands within
the C++ code, so I can enable or disable some function and so the using
of the libraries eg:

c++:
#ifdef lib1
#endif

#ifdef lib2
#endif

So my Java code must load lib1 and lib2 if both defs are set. Java does
not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java code
during the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"}). Do you know any
idea for this problem?

Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

I do not think it's worthwhile to open that can of worms for this
decision. After all getting rid of the CPP is one of the major
advantages of Java over C++ (even Stroustrup regrets not having ripped
it out of C++) and complicating the Java build process just for that
decision seems too heavy - especially since there are other solutions
available.

Kind regards

robert
 
A

Arne Vajhøj

I use within my Java code different external libraries (DLL), so I have
create a string array with their names
so the libraries can be load with System.load / System.loadLibrary.

I build the libraries (C++ code) with a toolchain and I use a lot of
preprocessor commands within
the C++ code, so I can enable or disable some function and so the using
of the libraries eg:

c++:
#ifdef lib1
#endif

#ifdef lib2
#endif

So my Java code must load lib1 and lib2 if both defs are set. Java does
not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java code
during the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"}). Do you know any
idea for this problem?

Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

I do not think it's worthwhile to open that can of worms for this
decision. After all getting rid of the CPP is one of the major
advantages of Java over C++ (even Stroustrup regrets not having ripped
it out of C++) and complicating the Java build process just for that
decision seems too heavy - especially since there are other solutions
available.

What other solution achieve the same?

Arne
 
R

Robert Klemme

On 12.10.2011 01:17, Arne Vajhøj wrote:
Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

I do not think it's worthwhile to open that can of worms for this
decision. After all getting rid of the CPP is one of the major
advantages of Java over C++ (even Stroustrup regrets not having ripped
it out of C++) and complicating the Java build process just for that
decision seems too heavy - especially since there are other solutions
available.

What other solution achieve the same?

Please see elsewhere in this thread.

Kind regards

robert
 
A

Arne Vajhøj

On 12.10.2011 01:17, Arne Vajhøj wrote:
Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

I do not think it's worthwhile to open that can of worms for this
decision. After all getting rid of the CPP is one of the major
advantages of Java over C++ (even Stroustrup regrets not having ripped
it out of C++) and complicating the Java build process just for that
decision seems too heavy - especially since there are other solutions
available.

What other solution achieve the same?

Please see elsewhere in this thread.

Well - they do not seem to be on my news server.

The only suggestion it carry are yours of:
* changing the logic, which may be a good idea, but is definitely
not the same
* use JNI to get the info from the native side, which will
require approx. 10 as much code than this

Arne
 
R

Robert Klemme

On 10/13/2011 12:35 PM, Robert Klemme wrote:
On 12.10.2011 01:17, Arne Vajhøj wrote:
Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

I do not think it's worthwhile to open that can of worms for this
decision. After all getting rid of the CPP is one of the major
advantages of Java over C++ (even Stroustrup regrets not having ripped
it out of C++) and complicating the Java build process just for that
decision seems too heavy - especially since there are other solutions
available.

What other solution achieve the same?

Please see elsewhere in this thread.

Well - they do not seem to be on my news server.

The only suggestion it carry are yours of:
* changing the logic, which may be a good idea, but is definitely
not the same

Do you say that because of
Java does not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java codeduring the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"})

? Right, I took the liberty to ignore the specific requirement to
change the Java code because I consider the ultimate task to solve to
only load those libs which can be used. Changing the Java code is just
one means to that end.
* use JNI to get the info from the native side, which will
require approx. 10 as much code than this

Well, but the solution to use CPP for Java code will probably need a
similar amount of code - even if it's not in Java but in ant scripts or
Makefiles. And it will be a dramatic change to the build process. One
also needs to decide whether to apply CPP to all Java files or just
selectively to a single one. The first option is probably cleaner from
a build process point of view but increases the risk of bugs (i.e. if
CPP starts replacing things in other files that it is not supposed to
change) while the second solution is an ugly crutch IMHO.

Kind regards

robert
 
A

Arne Vajhøj

On 10/14/2011 04:49 AM, Arne Vajhøj wrote:
On 10/13/2011 12:35 PM, Robert Klemme wrote:
On 12.10.2011 01:17, Arne Vajhøj wrote:

Run the Java code through the C preprocessor with the same defines.

String[] javaarray = {
"None"
#ifdef lib1
,"lib1"
#endif
#ifdef lib1
,"lib1"
#endif
};

I do not think it's worthwhile to open that can of worms for this
decision. After all getting rid of the CPP is one of the major
advantages of Java over C++ (even Stroustrup regrets not having ripped
it out of C++) and complicating the Java build process just for that
decision seems too heavy - especially since there are other solutions
available.

What other solution achieve the same?

Please see elsewhere in this thread.

Well - they do not seem to be on my news server.

The only suggestion it carry are yours of:
* changing the logic, which may be a good idea, but is definitely
not the same

Do you say that because of
Java does not support any preprocessor
flags, but in this case I need a tip for creating a flexiable Java
code during the class is compiled
(eg: ifdef1 = true => String[] javaarray = {"lib1"})

? Right, I took the liberty to ignore the specific requirement to change
the Java code because I consider the ultimate task to solve to only load
those libs which can be used. Changing the Java code is just one means
to that end.

Alternative approaches can be very good.

But it is an alternative approach.
Well, but the solution to use CPP for Java code will probably need a
similar amount of code - even if it's not in Java but in ant scripts or
Makefiles. And it will be a dramatic change to the build process. One
also needs to decide whether to apply CPP to all Java files or just
selectively to a single one. The first option is probably cleaner from a
build process point of view but increases the risk of bugs (i.e. if CPP
starts replacing things in other files that it is not supposed to
change) while the second solution is an ugly crutch IMHO.

It will require adding 1 line to the makefile/build script.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top