when trying to build i get this errors:
undefined reference to 'uuid_generate'
undefined reference to 'uuid_unparse'
this my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.6)
project(Synergy)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -luuid -pthread")
find_package (Threads)
set(SOURCE_FILES Functions.cpp Inotify.cpp Inotify.h main.cpp Master.h Message.cpp Message.h Server.cpp Server.h Threads.cpp )
add_executable(synergy_server ${SOURCE_FILES})
I can solve this problem by creating Makefile by myself and add -luuid flag.
but I want to do it with CMake,I tried to add this flag in the CMakeLists.txt file but it doesn't help :(
I have installed uuid-dev(it's not the Problem )
Hope you can help me.
Have a nice day
edit:
I add target_link_libraries(Synergy uuid) to the end of the file and it works, but there is a better way(look at the answers below)
This is not a good way to write a CMake script. CMake come with a rich set of functionalities that you must use to describe what you want to do.
In your case, you must avoid setting flags directly as you have done. In this way, you will be able to change compiler or even OS and your script will still work.
In your case, I would write something like this:
cmake_minimum_required(VERSION 3.6)
project(Synergy)
set(CMAKE_CXX_STANDARD 11)
# using pkg-config to configure uuid
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)
pkg_search_module(UUID REQUIRED uuid)
set(SOURCE_FILES Functions.cpp Inotify.cpp Inotify.h main.cpp Master.h message.cpp Message.h Server.cpp Server.h Threads.cpp )
add_executable(synergy_server ${SOURCE_FILES})
target_include_directories(synergy_server PUBLIC ${UUID_INCLUDE_DIRS})
target_link_libraries(synergy_server ${UUID_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
By the way, what means uuid-dev? I have no such thing in my system
Related
I want to give examples to some users of my project. The exmaple is to be used CMake. And It's like:
- top level
CMakeLists.txt
-- example A
A.cpp
CMakeLists.txt
-- example B
B.cpp
CMakeLists.txt
-- example C
C.cpp
CMakeLists.txt
I want to set some common CMake options in the top CMakeLists.txt, like:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -fexperimental-library")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lc++abi -fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -lc++abi -fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lc++abi -fuse-ld=lld")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
And I want that the example project can be built one time and the sub example can be built independently.
#A.cpp
cmake_minimum_required(VERSION 3.24)
# Something to be added
project(A)
add_executable(${PROJECT_NAME} A.cpp)
But I wish that I can do it without copy the common options into each CMakeLists.txt. I don't find a simple answer on StackOverflow or throughout Google. I think it should be a question may be met frequently in the fact. It's helpful to provide a tutorial even just a tutorial url.
Simply use add_subdirectory? Would simplify things and should still meet your requirement?
option(MYPROJECT_BUILD_EXAMPLES "Build examples" OFF/ON)
...
if (MYPROJECT_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
i'm trying to run a C game i found on youtube using CLion but i keep getting this error: fatal error: SDL2_gfxPrimitives.h: No such file or directory. i downloaded the SDL2_gfx library but seems like there's something wrong with the CMakeList.txt that can't find the file.
cmake_minimum_required(VERSION 3.0)
project(untitled C)
set(CMAKE_C_STANDARD 99)
set(SDL2_INCLUDE_DIR C:/SDL2/include)
set(SDL2_LIB_DIR C:/SDL2/lib/x86)
set(SDL2_GFX C:/SDL2_gfx)
include_directories(${SDL2_INCLUDE_DIR})
link_directories(${SDL2_LIB_DIR})
find_package(PkgConfig)
pkg_check_modules(SDL2_GFX SDL2_gfx)
include_directories(${SDL2_GFX_INCLUDE_DIRS})
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -Wall -Werror -fdump-rtl-expand")
# add_executable(untitled main.c)
set(SRCS
main.c
logic.c
rendering.c
)
set(HEADERS
logic.h
rendering.h
game.h
)
add_executable(untitled ${SRCS} ${HEADERS})
target_link_libraries(${PROJECT_NAME} SDL2main SDL2 ${SDL2_GFX_LIBRARIES})
If someone knows how to fix it, thanks in advance!!
Your question does not provide many importing bits of information, like the OS and Minimum Reproducible Example, but I'll to help you anyway.
If you use Linux, you need to install the sdl2_gfx package.
/usr/include/SDL2/SDL2_framerate.h
/usr/include/SDL2/SDL2_gfxPrimitives.h
/usr/include/SDL2/SDL2_imageFilter.h /usr/include/SDL2/SDL2_rotozoom.h
/usr/lib/libSDL2_gfx-1.0.so.0 /usr/lib/libSDL2_gfx-1.0.so.0.0.2
/usr/lib/libSDL2_gfx.so /usr/lib/pkgconfig/SDL2_gfx.pc
/usr/share/licenses/sdl2_gfx/LICENSE
When I compile my code using command line everything works fine:
g++ main.cpp -lpngwriter
But when I try using cmake I get undefined reference errors.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(myproject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpngwriter")
set(SOURCE_FILES main.cpp)
add_executable(myproject ${SOURCE_FILES})
Any ideas how to fix it?
Consider adding include_directories(/path/to/include) and link_directories(/path/to/lib) before add_executable().
And then insert target_link_libraries(pngwriter) after add_executable().
/path/to shall be replaced with relevant values for your system.
I'm new to JetBrains' CLion. Sorry if my question is basic but I didn't find any answer by searching.
#include <string>
int main() {
std::string str;
str.assign("ABC");
}
This very simple code compiles fine but the problem is the editor can't suggest member methods for str.
I'm using CLion 2016.3.2 on Fedora 25
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(ITP)
set(CMAKE_CXX_STANDARD "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(/usr/include /usr/local/include /usr/local/pgsql/include)
set(SOURCE_FILES
main.cpp
commander.cpp
)
add_custom_target(ITP command make -C /home/ben/projects/ITP)
Note: when I point to #include the editor didn't knows relevant include file!
Finally I reached to an answer!
Clion works with Cmake. there for CMakeLists.txt is very important.
many settings with cmake can effect code compilation, build types and ...
for more details please see my new CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(ITP)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
set(SOURCE_FILES
main.cpp
commander.cpp)
link_directories(/usr/local/pgsql/lib/)
message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
message("C Flags = ${CMAKE_CXX_FLAGS_DEBUG}")
add_executable(itp OrderManagementSystem/main.cpp)
add_custom_command(
TARGET itp POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/config.ini
${CMAKE_CURRENT_BINARY_DIR})
TARGET_LINK_LIBRARIES( itp
pthread
boost_system boost_thread
protobuf
zmq
pq
)
I'm new with both Clion and cmake. but these two ..
Unable to link protobuf library using CMake. My CMakeLists is
cmake_minimum_required(VERSION 3.6)
project(addressbook)
set(CMAKE_CXX_STANDARD 11)
set(PROJECT_NAME addressbook)
ADD_SUBDIRECTORY(proto)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(main main.cpp)
TARGET_LINK_LIBRARIES(main proto ${PROTOBUF_LIBRARY})
and in proto subdirectory there is another CMakeLists.txt (that way it is done in github repo https://github.com/shaochuan/cmake-protobuf-example)
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER message.proto)
ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})
But my IDE still outputs buch of lines like
CMakeFiles/main.dir/main.cpp.o: In function main':
/home/camille/ClionProjects/protobuf/main.cpp:42: undefined reference
togoogle::protobuf::internal::VerifyVersion(int, int, char const*)'
/home/camille/ClionProjects/protobuf/main.cpp:49: undefined reference
to tutorial::AddressBook::AddressBook()'
/home/camille/ClionProjects/protobuf/main.cpp:54: undefined reference
togoogle::protobuf::Message::ParseFromIstream(std::istream*)'
Where is my mistake? How do I make it work?
Your program fails to link because ${PROTOBUF_LIBRARY} is empty in the scope of your top-level CMakeLists.txt. This happens because calling add_subdirectory creates a child scope, and the Protobuf_XXX variables set by find_package(Protobuf REQUIRED) are only in that child scope.
A good way to fix this is to add the following to proto/CMakeLists.txt:
target_link_libraries(proto INTERFACE ${Protobuf_LIBRARIES})
This instructs targets that link to proto to also link to ${Protobuf_LIBRARIES}. Now you can simplify target_link_libraries in your top-level CMakeLists.txt:
target_link_libraries(addressbook proto)
On a side note, you could also use e.g.
target_link_libraries(${PROJECT_NAME} INTERFACE ... )
${PROJECT_NAME} resolves to whatever you have set in the project(...) statement in that CMakeLists.txt file.
Finally, note that this links to Protobuf_LIBRARIES instead of PROTOBUF_LIBRARY. Protobuf_LIBRARIES includes both the Protocol Buffers libraries and the dependent Pthreads library.
Watch out for variable name case: With CMake 3.6 and later, the FindProtobuf module input and output variables were all renamed from PROTOBUF_ to Protobuf_ (see release notes), so using Protobuf_ works with CMake 3.6, but fails with undefined reference with an earlier version.
To be on the safe side, either use the old style
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROTOBUF_LIBRARIES}))
or force everyone to use at least CMake 3.6
cmake_minimum_required(VERSION 3.6)
Also, there is a resolved bug report in the Kitware cmake issue tracker with more information on how to diagnose such issues.
The variable you need to pass to target_link_libraries is Protobuf_LIBRARIES. See documentation.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(protobuf)
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")
set(CMAKE_CXX_STANDARD 11)
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER addressbook.proto)
ADD_LIBRARY(proto2 ${PROTO_HEADER} ${PROTO_SRC})
TARGET_LINK_LIBRARIES(proto2)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
add_executable(protobuf main.cpp)
TARGET_LINK_LIBRARIES(protobuf proto2 ${PROTOBUF_LIBRARY})