1

I know there is no straight forward way to convert make files to cmake, but need some guidance. I have a make file for an embedded machine, and want to move forward using cmake.

I am cross-compiling on a linux dev machine for an embedded arm running a linux kernel.

currently, all files are in the same folder. I am working to move them to this structure: -build -documentation -includes -src CMakeLists.txt -test CMakeLists.txt

I want to put header files in the /includes folder, and the c/cpp files in the src folder.

OUT = enterprise.exe
CC = g++
CCFLAGS = -lrt -Wall -pthread

HEADERS = logobj.h message.h channel.h utilities.h parameter.h mycan.h vthread.h logger.h protocol_quest.h protocol_can.h protocol_io.h protocol_navitrol.h qt_nav.h qt_battery.h qt_iodealer.h enterprise.h
OBJECTS = logobj.o message.o channel.o utilities.o parameter.o mycan.o vthread.o logger.o protocol_quest.o protocol_can.o protocol_io.o protocol_navitrol.o qt_nav.o qt_battery.o qt_iodealer.o enterprise.o

GPIOHEADERS = evgpio.h 
GPIOOBJECTS = evgpio.o

%.o: %.c $(HEADERS)
        $(CC) $(CCFLAGS) -c -o $@ $<

%.o: %.c $(GPIOHEADERS)
        $(CC) $(CCFLAGS) -c -o $@ $< -mcpu=arm9

$(OUT): $(OBJECTS) $(GPIOOBJECTS)
        $(CC) $(CCFLAGS) -o $@ $^ $(LIB)

clean:
    -rm -f $(OBJECTS) $(GPIOOBJECTS)
    -rm -f $(OUT)

I am thinking it will look like this:

Top folder CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(enterprise)
set(CMAKE_CXX_COMPILER /opt/toolchains/arm-2008q3/bin/arm-none-linux-gnueabi-g++)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(COMPILE_FLAGS "-lrt -Wall -pthread")
add_subdirectory(src)

/src folder CMakeLists.txt

include_directories(${CMAKE_CURRENT_SOURCE_DIR})   //haven't moved the headers yet
set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR} filename1.cpp filename2.cpp  etc.cpp)
set(vendorLib filenameVendor1.o filenameVendor2.o etcVendor.o)

add_executable(enterprise ${SOURCES})
target_link_libraries(enterprise vendorLib)

Part of me thinks there should be something linking, but not sure where. At this time, I can get through cmake process, but fails on make. Undefined references and such...

Thoughts?

*****EDIT** Updated src/CMakeLists.txt to include comments and updates from testing.

wegunterjr
  • 141
  • 2
  • 9
  • `set(SOURCES ./*.c ./*.cpp)` The last time I checked you had to manually glob using the FILE command like this: http://stackoverflow.com/a/3201211/487892 – drescherjm Sep 01 '16 at 17:01
  • 2
    I don't use globbing I add every file to my source variables as I create new files. Even with my `CMake` projects that contain several thousand source files. – drescherjm Sep 01 '16 at 17:03
  • @drescherjm correct. Totally was guessing and hoping on that. I am putting in my actual files now. – wegunterjr Sep 01 '16 at 17:57
  • I edited the initial post. Now, it makes it through cmake, but when i try and make it, i get issues with somethings that it can't find. One of the source files is compiled to a .o ...how do i include that to help my project compile? – wegunterjr Sep 01 '16 at 19:36

1 Answers1

1

You need to add all source files to the executable:

add_executable(enterprise ${SOURCES})

Or at least add all source files needed to build the executable.

If you have libraries, then you need to add them too:

target_link_libraries(enterprise rt)

For more information please read the documentation.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Got ya. I guess my big question is I have .h files, is that what include_directories($CMAKE_CURRENT_SOURCE_DIR}) – wegunterjr Sep 01 '16 at 17:59
  • also, I have .o files from a vendor to interface with their system, does that fall under add_library(vendor vendor.o) etc... – wegunterjr Sep 01 '16 at 18:01
  • @wegunterjr How do you include them? Using quotes (like `#include "some_header_file.h"`) or using angle brackets (like `#include `)? If you use quotes then you don't need to do anything as then the preprocessor will look in the same directory as the source file to begin with. – Some programmer dude Sep 01 '16 at 18:02
  • Copy. I meant for cmake to use for compiling...or is it not necessary for cmake to know? – wegunterjr Sep 01 '16 at 18:04