I am new to CMake and it seems really hard to get my script work. My codes can be compiled the usual way, but I really need to use CMake. I compiled with the following:
g++ vectc.cpp -c -std=c++11
gfortran vectf.f vectc.o -lstdc++
This CMakeLists.txt, that doesn't work for me:
cmake_minimum_required (VERSION 2.6)
project (add_vectors CXX Fortran)
enable_language(Fortran)
set(CMAKE_CXX_FLAGS "-c -std=c++11")
set(CMAKE_Fortran_FLAGS "CMakeFiles/executable/vectc.o -lstdc++")
add_executable( executable
vectc.cpp
vectf.f)
If I run make after cmake i get the following, and I really dont know what to do:
[ 33%] Linking CXX executable executable
c++: warning: CMakeFiles/executable.dir/vectf.f.o: linker input file unused because linking not done
c++: warning: CMakeFiles/executable.dir/vectc.cpp.o: linker input file unused because linking not done
[100%] Built target executable
Does anyone can help me with it?
Edit:
The comments shows, I did not asked well. I am pretty new to the Cmake, and I don't know why I got the warnings. Also I have not found my "executable" file.
Reformulating my previous comment as an answer:
In case of mixed language sources (CXX, Fortran) the CXX linker is used by CMake because its linker preference is higher than that of the Fortran linker. But because of the PROGRAM statement in the fortran source the Fortran Linker is needed. Setting the LINKER_LANGUAGE property by set_property(TARGET executable PROPERTY LINKER_LANGUAGE Fortran) gives CMake a hint to select the correct linker.
Related
in order to use colormake I did set this alias in my .bashrc
alias make="/usr/bin/colormake"
It works, as if I try to compile (with qmake) a simple C++ example code with errors (just a main.cpp with a cout ), they are correctly coloured.
However, if I compile the same code with cmake, colormake is not used. What can I do to force cmake to use it?
my minimal CMakeList.txt example is
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2)
ADD_DEFINITIONS(-std=c++11)
ADD_EXECUTABLE(exe main.cpp)
System: Debian 8.8 jessie
Thanks, Valerio
Update:
I modified the CMakeLists.txt in this way, but no success:
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2)
ADD_DEFINITIONS(-std=c++11)
set(CMAKE_MAKE_PROGRAM /usr/bin/colormake)
ADD_EXECUTABLE(exe main.cpp)
message("CMAKE_MAKE_PROGRAM: " ${CMAKE_MAKE_PROGRAM})
Update 2:
I modified the CMakeList in this way:
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2)
ADD_DEFINITIONS(-std=c++11)
#set(CMAKE_COLOR_MAKEFILE OFF)
#set(CMAKE_MAKE_PROGRAM /usr/bin/colormake)
ADD_EXECUTABLE(exe main.cpp)
message("CMAKE_MAKE_PROGRAM: " ${CMAKE_MAKE_PROGRAM})
message("CMAKE_COLOR_MAKEFILE: " ${CMAKE_COLOR_MAKEFILE})
then launched cmake with this argument from command line:
cmake -DCMAKE_MAKE_PROGRAM=/usr/bin/colormake -DCMAKE_COLOR_MAKEFILE=OFF ../
But again, the main.cpp synthax error after make is not coloured.
This is the output of cmake, note the messages about CMAKE_MAKE_PROGRAM and CMAKE_COLOR_MAKEFILE
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMAKE_MAKE_PROGRAM: /usr/bin/colormake
CMAKE_COLOR_MAKEFILE: OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /home/valeriosperati/Desktop/VALERIO_SPERATI/prova_codice_c/colormake/cmake/build
Some additional (maybe helpful) info: this is the output
I obtain when compiling with qmake, the error 'hjskf' is in red.
this is the output when comiling with cmake:
It should be enough to set the CMAKE_MAKE_PROGRAM cache variable to point at the build tool you want to use. You can do this by running cmake with a -D option like so:
cmake -DCMAKE_MAKE_PROGRAM=/usr/bin/colormake path/to/src
For the benefit of others, this technique can be used with other generators too, not just make. Just be aware that it is your responsibility to make sure the build tool specified matches the generator type CMake is using (i.e. don't pass a make tool if you've told CMake to use Ninja with -G Ninja instead).
Note, however, that this only really matters if you are invoking the build via CMake like so:
cmake --build path/to/build/dir
Some IDE tools may invoke the build that way. Most of the time, however, developers invoke the tool directly. In your case, you can simply invoke colormake instead of make. If you are still not getting colored output after doing that, then your problem must be elsewhere (check your terminal type settings perhaps).
I have a library that must be linked to 2 main codes, one in fortran and on in cxx.
I have this library in src/lib/CMakeLists.txt:
ADD_LIBRARY(mylib a.f90 b.c c.cc)
and in src/main/CMakeLists.txt, I have:
ADD_EXECUTABLE(mymain1 mymain1.f90)
TARGET_LINK_LIBRARIES(mymain1 mylib)
ADD_EXECUTABLE(mymain2 mymain2.cc)
TARGET_LINK_LIBRARIES(mymain2 mylib)
When compiling mymain1, it use the CXX compiler to link instead of the Fortran one.
How can I tell cmake to use Fortran to link mymain1 and CXX to link mymain2?
You may directly affect on language used for linking with LINKER_LANGUAGE property:
# Use Fortran compiler for link 'mymain1' executable
set_target_properties(mymain1 PROPERTIES LINKER_LANGUAGE Fortran)
Another way could be "teach" CMake to properly choose linker.
Without the library CMake would correctly select Fortran linker for mymain1 as it compiled only from Fortran sources, and C++ linker for mymain2 as it compiled only from C++ sources.
But linking with the library messes CMake: because the library mylib is compiled from sources on several languages, CMake selects linker for it using some "preference scores" for languages (see CMAKE_<LANG>_LINKER_PREFERENCE variable). More likely, C++ "beats" Fortran in your case.
Moreover, when select linker language for mymain1, CMake take into account language for the static library mylib. Because of that C++ wins even for the executable built from Fortran sources only.
You may disable propagating library's language to the executable using variables CMAKE_<LANG>_LINKER_PREFERENCE_PROPAGATES:
# <place this *before* adding executables>
# Do not propagate language of C++ libraries to the executables.
set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES OFF)
When CMake is used for a mixed language project (C/C++ and FORTRAN), the C++ compiler is called to link the executable. Is there an easy way to call the FORTRAN compiler for the linking step.
project(Serialbox_Fortran_Perturbation_Example CXX Fortran)
add_executable(main_producer main_producer.f90 m_ser.f90)
This will compile correctly with the FORTRAN compiler but for the linking step, the C++ compiler will be called and it causes trouble with some compiler suite like PGI for example.
As a workaround one can set the linker language explicitly:
set_property(TARGET your_target PROPERTY LINKER_LANGUAGE Fortran)
or play with CMAKE_<LANG>_LINKER_PREFERENCE (I haven't checked if the latter works now, it didn't work when I tried a few years ago).
I expect what you are seeing is that the linkage is executed through the GCC C++ frontend
with Fortran libraries added. To get the linkage done through the GCC Fortran frontend
this hack should do:
project(Serialbox_Fortran_Perturbation_Example CXX Fortran)
set(CMAKE_CXX_LINK_EXECUTABLE "gfortran <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
add_executable(main_producer main_producer.f90 m_ser.f90)
I'm writing my first cmake file from scratch for a project that requires, amongst other things, ODE. If there are other bad practices that I'm doing, comments on that are also very welcome.
The makefile generation step with cmake is working, however in the build step with make, it complains:
Scanning dependencies of target ode_oscillex
[ 50%] Building CXX object CMakeFiles/ode_oscillex.dir/ode_oscillex.cpp.o
[100%] Linking CXX executable ode_oscillex
ld: library not found for -lGLU
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [ode_oscillex] Error 1
make[1]: *** [CMakeFiles/ode_oscillex.dir/all] Error 2
make: *** [all] Error 2
And I can't figure out what library -lGLU is supposed to be. Based on the Eclipse setup this project was previously set up in, I'm already adding X11, OpenGL and GLUT, and there shouldn't be anything else additionally. I'm setting this up on OS X.
My CMakeLists.txt is
cmake_minimum_required (VERSION 3.3)
set(PROJECT_NAME ode_oscillex)
set(TARGET ${PROJECT_NAME})
project (${PROJECT_NAME})
# Add manually built ODE library
set(ODE_PATH /Users/steve/ode-0.13.1)
set(ODE_INC_PATH ${ODE_PATH}/include)
set(ODE_LIB_PATH ${ODE_PATH}/ode/src/.libs)
set(DS_INC_PATH ${ODE_PATH}/include/drawstuff) # not sure why drawstuff needs subfolder to be specified, when ode doesn't...
set(DS_LIB_PATH ${ODE_PATH}/drawstuff/src/.libs)
include_directories(${ODE_INC_PATH} ${DS_INC_PATH})
link_directories(${ODE_LIB_PATH} ${DS_LIB_PATH})
### Add manually built Eigen
SET( EIGEN3_INCLUDE_DIR ../eigen3.1.4) # This is also sucky
include_directories(${EIGEN3_INCLUDE_DIR})
### Add Executable
add_executable(${TARGET} ${PROJECT_NAME}.cpp)
### Adding other required libraries
find_package(X11 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${X11_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${X11_LIBRARIES})
target_link_libraries(${TARGET} ode drawstuff GLU GL glut X11)
The problematic instructions for me are:
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${X11_LIBRARIES})
target_link_libraries(${TARGET} ode drawstuff GLU GL glut X11)
Generally, you shouldn't link "project", you need to link
"project artifacts" (i.e. "${TARGET}").
So try to rewrite this as follows:
target_link_libraries(${TARGET} <other required libraries like "ode"...> {OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${X11_LIBRARIES})
and remove the other line completely.
Generally when you called find_package()s above, those macros filled in corresponding variables which tell how to properly get proper compiler and linker flags (for OpenGL, GLUT etc respectively). So when you use appropriate variables in the target_link_libraries invocation, you can be sure that the linker flags and libraries, specific to a particular system.
On the the other hand, when you simply use target_link_libraries(<target> GLU GL glut...) you forcibly instruct cmake to link your <target> against those GL-related libraries, no matter if they installed in a particular system, nor how they are named there.
So in general you should prefer the first approach for external libraries, it's more reliable and generic.
Visual Studio C++ 2008 / GCC 4.4.2
I have written a program to run on Linux and now I have to port my code to run on Windows. I have decided to use CMake as I want to keep the same build system for both platforms.
However, I need to link with some libraries for both platforms. In my CMakeLists.txt I have the following:
# Compile with gcc c89 standard
IF(CMAKE_COMPILER_IS_GNUCXX)
MESSAGE(STATUS "GCC detected - Adding compiler flags")
SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall -pedantic -std=c89")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
IF(WIN32)
SET(CMAKE_C_FLAGS "ws2_32.lib")
ENDIF(WIN32)
However, when I compile on Visual Studio I get the following error:
fatal error C1083: Cannot open source file: 'ws2_32.lib': No such file or directory
What can I do to resolve this problem?
========= Edit
In the top level directory
# Project Client Server
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
# Name of project
PROJECT(CLIENT_SERVER)
# Compile with gcc c89 standard
IF(CMAKE_COMPILER_IS_GNUCXX)
MESSAGE(STATUS "GCC detected - Adding compiler flags")
SET(CMAKE_C_FLAGS "-pthread -ggdb -Wextra -Wall -pedantic -std=c89")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
IF(WIN32)
SET(CMAKE_C_FLAGS "ws2_32")
ENDIF(WIN32)
# Includes
INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/cltsvr_ults)
INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/server)
INCLUDE_DIRECTORIES(${CLIENT_SERVER_SOURCE_DIR}/client)
# libraries
LINK_DIRECTORIES($CLIENT_SERVER/cltsvr_ults)
# Add subdirectories
ADD_SUBDIRECTORY(client)
ADD_SUBDIRECTORY(server)
ADD_SUBDIRECTORY(cltsvr_ults)
ADD_SUBDIRECTORY(test_client)
ADD_SUBDIRECTORY(test_server)
In the subdirectory of client I have this CMakeLists.txt
# libray called client from client.c
ADD_LIBRARY(client client)
And in the subdirectory of test_clt where I create and link my executable.
# Test client add executable
INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/client)
INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/cltsvr_ults)
# Link the library
LINK_DIRECTORIES($CLIENT_SERVER/client)
# Add the executable
ADD_EXECUTABLE(clt test_clt)
# Link the executable to the client library
IF(WIN32)
TARGET_LINK_LIBRARIES(clt client ws2_32)
ENDIF(WIN32)
Disclaimer: My answer is of philosophical nature which should encourage you to avoid touching CMAKE_C_FLAGS directly. For the direct answer that just solves your problem look what Bill ( the lead architect of the CMake btw. ) wrote.
The thing about CMake is, that it lets you describe what you want to do without referring to a specific compiler or platform. What CMake does is building the compiler and linker flags from your usage of
include_directories
add_definitions
add_library
add_executable
target_link_libraries
If there are no external dependencies, other than the compiler itself, this is all you need. For external dependencies use
find_package
It defines a set of variables, like
find_package(SDL)
defines
SDL_INCLUDE_DIR
SDL_LIBRARY
for usage with respectively include_directories and target_link_libraries. CMake ships with a bunch of so called module files, like FindSDL.cmake and many others can be googled.
The next lower level is to use
find_path
find_library
which are used in the Find???.cmake modules itself.
The CMAKE_C_FLAGS variable is composed by CMake from these commands. Modifying it means you bypass CMake. There are cases, like for special optimization flags, you want to do this, but at this point all power and thus responsibility transfered from CMake to you.
By adding ws2_32.lib to the C_FLAGS, you are using it at compile time, and not link time. If you look at the error message, you can see it it treating the file as if it were a c++ source file: Cannot open source file: 'ws2_32.lib'. target_link_libraries(target ws2_32) should work.
You need to use the Target Link Libraries command. The target would be the executable you're building.
EDIT: You shouldn't specify the libs you're linking against in C_FLAGS. You can do something like TARGET_LINK_LIBRARIES(execName, ws_32, ...). I'm not 100% sure if you need the .lib. Been a while since I used CMake.