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? :)
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.
Comments