From Makefile to Cmake

stkubr

because of CLion release i'd like to move from make to CMake. So i have source folder with bunch of .cpp and .hpp files and a main.cpp with uses classes from source above - so far i had easy Makefile to compile, link and make exec out of main.cpp:

C_OBJS := $(shell find source/*/ -name '*.cpp')
SOURCES=source/main.cpp $(C_OBJS)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=bin/main

$(EXECUTABLE_T): $(OBJECTS_T) 
$(CC) $(LDFLAGS) $(OBJECTS_T) -o $@

$(EXECUTABLE): $(OBJECTS) 
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

%.o: %.cpp 
$(CC) $(CFLAGS) $< -c -o $@

all: $(EXECUTABLE)

In Cmake i came to something like that:

cmake_minimum_required(VERSION 2.8.4)
project(Main)

#grab all file we need to compile
file(GLOB SRC
    "source/*.h"
    "source/*.hpp"
    "source/*.cpp"
)

#compile to .o files all we need
add_library( myLib ${SRC} )

set(SOURCE_FILES source/main.cpp)

#compile to .o main.cpp
add_executable( Main ${SOURCE_FILES} )

#link them all
target_link_libraries( Main myLib )

But i keep getting "undefined reference to" to all my classes at the linking and judging by compilation time feels like not everything is compiled. Could you help? :)

David Faure

Try making the library static rather than shared.

add_library( myLib STATIC ${SRC} )

Otherwise you're getting a shared library by default, which requires proper export macros.

Without export macros, the symbols (e.g. methods) from the lib are not exported, and therefore not available at link time. Proper export macros are always necessary for shared libs on Windows, and are necessary for shared libs on Unix when -fvisibility=hidden is set, which is usually the case.

Also, as someone else said, don't use GLOB but list your files explicitely, as per http://www.cmake.org/cmake/help/v3.0/command/file.html. But that's a matter of good practice, unrelated to your actual link error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic rule from makefile to CMake

From Dev

CMake change color in makefile

From Dev

Conversion of Makefile to CMake

From Dev

CMake change color in makefile

From Dev

Transforming makefile to cmake

From Dev

Conversion of Makefile to CMake

From Dev

Use makefile variables in CMake script

From Dev

cmake to create Makefile with *.cpp rule

From Dev

cmake to create Makefile with *.cpp rule

From Dev

Makefile: Run other makefile from makefile

From Dev

Preventing CMake-generated makefile for optional-header-only library from compiling source files in header-only mode

From Java

Using local makefile for CLion instead of CMake

From Java

Difference between using Makefile and CMake to compile the code

From Dev

Is it possible to change the name of generated makefile in CMake?

From Dev

cmake -- let a subdirectory use its own Makefile

From Dev

Cross platform alternative `mkdir -p` in makefile + cmake

From Dev

Why isn't the command "cmake ." generating a makefile?

From Dev

cmake: pregenerate Makefile for non-existing sources

From Dev

Makefile generator specification at cross compilation with CMake

From Dev

Cross platform alternative `mkdir -p` in makefile + cmake

From Dev

CMake-generated Makefile does not contain target

From Dev

Converting makefile with circular link dependency to CMake

From Dev

Converting Makefile to CMake using Flex and Bison

From Dev

Why CMake generate Makefile without CMAKE_CXX_FLAGS?

From Dev

Removing dependencies from a Makefile

From Dev

Overwrite variable from makefile

From Dev

Control output from makefile

From Dev

How to include a cmake Makefile after a target has been executed?

From Dev

Silence custom command depending on CMAKE_VERBOSE_MAKEFILE

Related Related

  1. 1

    Generic rule from makefile to CMake

  2. 2

    CMake change color in makefile

  3. 3

    Conversion of Makefile to CMake

  4. 4

    CMake change color in makefile

  5. 5

    Transforming makefile to cmake

  6. 6

    Conversion of Makefile to CMake

  7. 7

    Use makefile variables in CMake script

  8. 8

    cmake to create Makefile with *.cpp rule

  9. 9

    cmake to create Makefile with *.cpp rule

  10. 10

    Makefile: Run other makefile from makefile

  11. 11

    Preventing CMake-generated makefile for optional-header-only library from compiling source files in header-only mode

  12. 12

    Using local makefile for CLion instead of CMake

  13. 13

    Difference between using Makefile and CMake to compile the code

  14. 14

    Is it possible to change the name of generated makefile in CMake?

  15. 15

    cmake -- let a subdirectory use its own Makefile

  16. 16

    Cross platform alternative `mkdir -p` in makefile + cmake

  17. 17

    Why isn't the command "cmake ." generating a makefile?

  18. 18

    cmake: pregenerate Makefile for non-existing sources

  19. 19

    Makefile generator specification at cross compilation with CMake

  20. 20

    Cross platform alternative `mkdir -p` in makefile + cmake

  21. 21

    CMake-generated Makefile does not contain target

  22. 22

    Converting makefile with circular link dependency to CMake

  23. 23

    Converting Makefile to CMake using Flex and Bison

  24. 24

    Why CMake generate Makefile without CMAKE_CXX_FLAGS?

  25. 25

    Removing dependencies from a Makefile

  26. 26

    Overwrite variable from makefile

  27. 27

    Control output from makefile

  28. 28

    How to include a cmake Makefile after a target has been executed?

  29. 29

    Silence custom command depending on CMAKE_VERBOSE_MAKEFILE

HotTag

Archive