Discussion:
[CMake] FW: building a mixed language programm with CMake
Natalie Happenhofer
2009-06-09 13:31:04 UTC
Permalink
Hi everybody,
IŽd like to build a mixed language programm with CMake, more specifically, I have a C++ and a Fortran90 source file I want to link:
tp500main.cxx and electro1.f90

IŽm just wondering how it is done with CMake. The point is, I need to compile the sources separately with the corresponding compiler and then link it together.
I tried the following:

cmake_minimum_required(VERSION 2.6)
project(tp500)

enable_language(Fortran)

try_compile(electro1.f90)
try_compile(tp500main.cpp)

target_link_libraries(tp500 electro1.o tp500main.o)


which is, I suppose, a rather crude approach, but I hope it shows what IŽm trying to do..

thanks a lot for any piece of advice,
Natalie Happenhofer




_________________________________________________________________
Disfruta los mejores videos de MSN mientras chateas
http://messengertv.msn.com/mkt/es-xl/
Denis Scherbakov
2009-06-09 13:55:42 UTC
Permalink
This post might be inappropriate. Click to display it.
Denis Scherbakov
2009-06-10 09:38:58 UTC
Permalink
Thanks, that helped, but I still have troubles to set the
Compiling my code "manually", it takes the
gfortran -c hello.f90
g++ -lgfortran hello.o test.cxx -o test
Actually this should look like:
gfortran -c hello.f90
g++ -c test.cxx
g++ -lgfortran hello.o test.o -o test
cmake_minimum_required(VERSION 2.4)
project(TEST Fortran)
SET(TEST_SRCS test.cxx)
SET(OBJECTS hello.f90)
You need to work only with sources. Forget about object files.
Here is your CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(TEST Fortran)

SET(TEST_SRCS test.cxx hello.f90)

ADD_EXECUTABLE(test ${TEST_SRCS})
TARGET_LINK_LIBRARIES(test gfortran)


# NOT NEDDED: SET_TARGET_PROPERTIES(hi PROPERTIES LINKER_LANGUAGE C)
# NOT NEEDED: CMAKE_EXE_LINKER_FLAGS: -lgfortran -o
# Only if you have one more fortran compiler: CMAKE_Fortran_COMPILER: /usr/bin/gfortran
# NOT NEEDED: CMAKE_Fortran_FLAGS: -c
which looks fine, but if my executable is build, where does
it go? I set the EXECUTABLE_OUTPUT_PATH to the directory
I´m working in,
but the executable "hi" does not show up..
Compiled executable now is in build directory. In your case it should
be in current directory.

Please, reply to all, so CMake mailing list gets also included.

Denis
Natalie Happenhofer
2009-06-10 11:54:01 UTC
Permalink
The error has changed ;-) No seriously, I stll get an error buiding with CMake:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

Building from the command line works perfect, so I looked what libraries the executable is linked to:

linux-vdso.so.1 => (0x00007fff331ff000)
libgfortran.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/libgfortran.so.1 (0x00007ff22ac5e000)
libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/libstdc++.so.6 (0x00007ff22a962000)
libm.so.6 => /lib/libm.so.6 (0x00007ff22a6e1000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007ff22a4d4000)
libc.so.6 => /lib/libc.so.6 (0x00007ff22a195000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff22aef7000)

The only library I tell CMake to link to is libgfortran.so. What libraries does CMake usually link to, without specifying?

greets,
Natalie
Date: Wed, 10 Jun 2009 02:38:58 -0700
Subject: RE: [CMake] FW: building a mixed language programm with CMake
Thanks, that helped, but I still have troubles to set the
Compiling my code "manually", it takes the
gfortran -c hello.f90
g++ -lgfortran hello.o test.cxx -o test
gfortran -c hello.f90
g++ -c test.cxx
g++ -lgfortran hello.o test.o -o test
cmake_minimum_required(VERSION 2.4)
project(TEST Fortran)
SET(TEST_SRCS test.cxx)
SET(OBJECTS hello.f90)
You need to work only with sources. Forget about object files.
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(TEST Fortran)
SET(TEST_SRCS test.cxx hello.f90)
ADD_EXECUTABLE(test ${TEST_SRCS})
TARGET_LINK_LIBRARIES(test gfortran)
# NOT NEDDED: SET_TARGET_PROPERTIES(hi PROPERTIES LINKER_LANGUAGE C)
# NOT NEEDED: CMAKE_EXE_LINKER_FLAGS: -lgfortran -o
# Only if you have one more fortran compiler: CMAKE_Fortran_COMPILER: /usr/bin/gfortran
# NOT NEEDED: CMAKE_Fortran_FLAGS: -c
which looks fine, but if my executable is build, where does
it go? I set the EXECUTABLE_OUTPUT_PATH to the directory
IŽm working in,
but the executable "hi" does not show up..
Compiled executable now is in build directory. In your case it should
be in current directory.
Please, reply to all, so CMake mailing list gets also included.
Denis
_________________________________________________________________
Prueba los mejores juegos online en MSN
http://juegos.latam.msn.com/
Denis Scherbakov
2009-06-10 12:50:11 UTC
Permalink
Post by Natalie Happenhofer
(.text+0x20): undefined reference to `main'
Try to compile with VERBOSE option:

make VERBOSE=1

this will show you, what GCC does.
Post by Natalie Happenhofer
The only library I tell CMake to link to is libgfortran.so.
What libraries does CMake usually link to, without
specifying?
None. CMake is a Makefile generator it does not compile or link.
Could you run "nm test.o" and "nm hello.o" and post the result?
Also post verbose output.

Denis
Natalie Happenhofer
2009-06-10 13:31:58 UTC
Permalink
Hi!
I have run the commands:

$ nm test.o
U __gxx_personality_v0
U hello1_
0000000000000000 T main
$ nm hello.o
U _gfortran_st_write
U _gfortran_st_write_done
U _gfortran_transfer_character
0000000000000000 T hello1_

$ make VERBOSE=1
/usr/bin/cmake -H/home/happenhofer/versuche/CMake_tryout/Fortran -B/home/happenhofer/versuche/CMake_tryout/Fortran --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/happenhofer/versuche/CMake_tryout/Fortran/CMakeFiles /home/happenhofer/versuche/CMake_tryout/Fortran/CMakeFiles/progress.make
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make -f CMakeFiles/hi.dir/build.make CMakeFiles/hi.dir/depend
make[2]: Entering directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
cd /home/happenhofer/versuche/CMake_tryout/Fortran && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/happenhofer/versuche/CMake_tryout/Fortran /home/happenhofer/versuche/CMake_tryout/Fortran /home/happenhofer/versuche/CMake_tryout/Fortran /home/happenhofer/versuche/CMake_tryout/Fortran /home/happenhofer/versuche/CMake_tryout/Fortran/CMakeFiles/hi.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make -f CMakeFiles/hi.dir/build.make CMakeFiles/hi.dir/requires
make[2]: Entering directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make[2]: Nothing to be done for `CMakeFiles/hi.dir/requires'.
make[2]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make -f CMakeFiles/hi.dir/build.make CMakeFiles/hi.dir/build
make[2]: Entering directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
Linking Fortran executable hi
/usr/bin/cmake -E cmake_link_script CMakeFiles/hi.dir/link.txt --verbose=1
/usr/bin/gfortran -fPIC CMakeFiles/hi.dir/hello.o -o hi -rdynamic -lgfortran
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make[2]: *** [hi] Error 1
make[2]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make[1]: *** [CMakeFiles/hi.dir/all] Error 2
make[1]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make: *** [all] Error 2


Thanks a lot for your effort!
Natalie
Date: Wed, 10 Jun 2009 05:50:11 -0700
Subject: Re: [CMake] FW: building a mixed language programm with CMake
Post by Natalie Happenhofer
(.text+0x20): undefined reference to `main'
make VERBOSE=1
this will show you, what GCC does.
Post by Natalie Happenhofer
The only library I tell CMake to link to is libgfortran.so.
What libraries does CMake usually link to, without
specifying?
None. CMake is a Makefile generator it does not compile or link.
Could you run "nm test.o" and "nm hello.o" and post the result?
Also post verbose output.
Denis
_________________________________________________________________
Vuelve famosa a tu mascota con MSN
http://events.latam.msn.com/tumascota
Arjen Markus
2009-06-10 13:51:41 UTC
Permalink
Hello Natalie,

the linkstep in your makefile tries to create a complete program
out of hello.o, but the main program is in test.o.

In the PLplot project (http://plplot.sf.net) we have a similar
configuration: different programming languages that need to
be combined. The approach we take is:
- create a library (static or dynamic) from the sources in one
programming language
- create programs using these separate libraries

While I can not come up right now with the ready made CMakeLists.txt
file, I think that is the way to do it:
- turn hello.f90 into a library (say libhello.a)
- create the program from test.cpp using that library as a dependency.

Regards,

Arjen
Post by Natalie Happenhofer
Hi!
$ nm test.o
U __gxx_personality_v0
U hello1_
0000000000000000 T main
$ nm hello.o
U _gfortran_st_write
U _gfortran_st_write_done
U _gfortran_transfer_character
0000000000000000 T hello1_
$ make VERBOSE=1
/usr/bin/cmake -H/home/happenhofer/versuche/CMake_tryout/Fortran
-B/home/happenhofer/versuche/CMake_tryout/Fortran --check-build-system
CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start
/home/happenhofer/versuche/CMake_tryout/Fortran/CMakeFiles
/home/happenhofer/versuche/CMake_tryout/Fortran/CMakeFiles/progress.make
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory
`/home/happenhofer/versuche/CMake_tryout/Fortran'
make -f CMakeFiles/hi.dir/build.make CMakeFiles/hi.dir/depend
make[2]: Entering directory
`/home/happenhofer/versuche/CMake_tryout/Fortran'
cd /home/happenhofer/versuche/CMake_tryout/Fortran && /usr/bin/cmake -E
cmake_depends "Unix Makefiles"
/home/happenhofer/versuche/CMake_tryout/Fortran
/home/happenhofer/versuche/CMake_tryout/Fortran
/home/happenhofer/versuche/CMake_tryout/Fortran
/home/happenhofer/versuche/CMake_tryout/Fortran
/home/happenhofer/versuche/CMake_tryout/Fortran/CMakeFiles/hi.dir/DependInfo.cmake
--color=
make[2]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make -f CMakeFiles/hi.dir/build.make CMakeFiles/hi.dir/requires
make[2]: Entering directory
`/home/happenhofer/versuche/CMake_tryout/Fortran'
make[2]: Nothing to be done for `CMakeFiles/hi.dir/requires'.
make[2]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make -f CMakeFiles/hi.dir/build.make CMakeFiles/hi.dir/build
make[2]: Entering directory
`/home/happenhofer/versuche/CMake_tryout/Fortran'
Linking Fortran executable hi
/usr/bin/cmake -E cmake_link_script CMakeFiles/hi.dir/link.txt --verbose=1
/usr/bin/gfortran -fPIC CMakeFiles/hi.dir/hello.o -o hi -rdynamic -lgfortran
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make[2]: *** [hi] Error 1
make[2]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make[1]: *** [CMakeFiles/hi.dir/all] Error 2
make[1]: Leaving directory `/home/happenhofer/versuche/CMake_tryout/Fortran'
make: *** [all] Error 2
Thanks a lot for your effort!
Natalie
Date: Wed, 10 Jun 2009 05:50:11 -0700
Subject: Re: [CMake] FW: building a mixed language programm with CMake
Post by Natalie Happenhofer
(.text+0x20): undefined reference to `main'
make VERBOSE=1
this will show you, what GCC does.
Post by Natalie Happenhofer
The only library I tell CMake to link to is libgfortran.so.
What libraries does CMake usually link to, without
specifying?
None. CMake is a Makefile generator it does not compile or link.
Could you run "nm test.o" and "nm hello.o" and post the result?
Also post verbose output.
Denis
------------------------------------------------------------------------
¡Es hora que descubras quién eres! Alguien puede conocerte mejor que tu
mismo. <http://www.descubrewindowslive.com/>
------------------------------------------------------------------------
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
http://www.cmake.org/mailman/listinfo/cmake
Denis Scherbakov
2009-06-10 13:59:33 UTC
Permalink
Post by Natalie Happenhofer
$ nm test.o
U __gxx_personality_v0
U hello1_
0000000000000000 T main
$ nm hello.o
U _gfortran_st_write
U _gfortran_st_write_done
U _gfortran_transfer_character
0000000000000000 T hello1_
So you're calling Fortran from C.
Post by Natalie Happenhofer
/usr/bin/gfortran   -fPIC 
CMakeFiles/hi.dir/hello.o  -o hi -rdynamic -lgfortran
The problem is that CMake decides to use gfortran as a linker, not gcc,
because we specified that this is a Fortran project and forgot about C++.
So the solution is:

# To indicate that this is mixed language project
PROJECT(TEST CXX Fortran)
# To explicitly specify that your MAIN is C++'s main.
SET_TARGET_PROPERTIES(test PROPERTIES LINKER_LANGUAGE CXX)

"set_target_props" without "project" will not work.

Denis
Natalie Happenhofer
2009-06-11 09:28:03 UTC
Permalink
Great, now it works!
My CMakeLists.txt now looks like this:

cmake_minimum_required(VERSION 2.4)
project(hi CXX Fortran)

ADD_LIBRARY(hello SHARED hello.f90)
SET(TEST_SRCS test.cxx)

ADD_EXECUTABLE(hi ${TEST_SRCS})
TARGET_LINK_LIBRARIES(hi hello)
SET_TARGET_PROPERTIES(hi PROPERTIES LINKER_LANGUAGE CXX)

Thanks a lot for your help!
Greetings,
Natalie
Date: Wed, 10 Jun 2009 06:59:33 -0700
Subject: Re: [CMake] FW: building a mixed language programm with CMake
Post by Natalie Happenhofer
$ nm test.o
U __gxx_personality_v0
U hello1_
0000000000000000 T main
$ nm hello.o
U _gfortran_st_write
U _gfortran_st_write_done
U _gfortran_transfer_character
0000000000000000 T hello1_
So you're calling Fortran from C.
Post by Natalie Happenhofer
/usr/bin/gfortran -fPIC
CMakeFiles/hi.dir/hello.o -o hi -rdynamic -lgfortran
The problem is that CMake decides to use gfortran as a linker, not gcc,
because we specified that this is a Fortran project and forgot about C++.
# To indicate that this is mixed language project
PROJECT(TEST CXX Fortran)
# To explicitly specify that your MAIN is C++'s main.
SET_TARGET_PROPERTIES(test PROPERTIES LINKER_LANGUAGE CXX)
"set_target_props" without "project" will not work.
Denis
_________________________________________________________________
Gratis Internet Explorer 8 optimizado para MSN
http://www.ie8.msn.com/microsoft/internet-explorer-8/es-xl/ie8.aspx
Alexander Neundorf
2009-06-17 21:13:41 UTC
Permalink
Post by Natalie Happenhofer
Great, now it works!
cmake_minimum_required(VERSION 2.4)
project(hi CXX Fortran)
Just as a comment, with cmake 2.6 a lot of work has gone into Fortran support,
so maybe you should have a look at cmake >= 2.6.0 and require it if you find
it necessary.

Alex

Denis Scherbakov
2009-06-11 14:49:34 UTC
Permalink
Post by Natalie Happenhofer
Great, now it works!
I would put everything in an executable, without an intermediate step with
ADD_LIBRARY. When I tested my suggestions for you on my computer, they
worked without any intermediate library. But you might have some other
project in mind, so it might be a matter of taste.

I would also use STATIC instead of SHARED, but it depends on what you need.

Denis
Loading...