Converting Makefile to CMake with local includes and libs - gcc

I am unsure of how to add my include and libs files to a cmake file. I keep seeing references to several methods that I do not understand when trying to figure this out. I need to get my /include and /lib directories into CMake properly so I can use them in my code. What is the proper way of adding libraries to CMake?
OBJS = simple.o
INCLUDES = -I../../include
LIBS = -L../../lib
LDFLAGS = -lglad -lglfw3
PROGRAM = simple
CFLAGS = -g
COMPILER = g++
ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
LDFLAGS += -lopengl32 -lgdi32
PROGRAM :=$(addsuffix .exe,$(PROGRAM))
else ifeq ($(shell uname -s),Darwin) # is MACOSX
LDFLAGS += -framework Cocoa -framework OpenGL -framework IOKit
endif
simple: clean $(OBJS)
$(COMPILER) -o $(PROGRAM) $(OBJS) $(LIBS) $(LDFLAGS)
simple.o: simple.c
$(COMPILER) $(INCLUDES) $(CFLAGS) -c simple.c
RM = rm -f
ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
RM := del
endif
clean:
$(RM) $(OBJS) $(PROGRAM)
This is all I've got so far:
cmake_minimum_required(VERSION 3.23)
project(hw2)
set(CMAKE_CXX_STANDARD 17)
add_library(bar SHARED IMPORTED)
add_executable(hw2 main.cpp)

A simple example for you:
The files in this example are below:
tree
.
├── CMakeLists.txt
├── include
│   └── shared
│   └── Hello.h
└── src
├── Hello.cpp
└── main.cpp
CMakeLists.txt[] - Contains the CMake commands you wish to run
include/shared/Hello.h[] - The header file to include
src/Hello.cpp[] - A source file to compile
src/main.cpp[] - The source file with main
STEP 1: Specify the minimum version of CMake and create a project
cmake_minimum_required(VERSION 3.5)
project(hello_library)
STEP 2: Add a Shared Library using add_library
add_library(hello_library SHARED
src/Hello.cpp
)
STEP 3: Set the directories that should be included using target_include_directories
target_include_directories(hello_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
STEP 4: Add an executable
add_executable(hello_binary
src/main.cpp
)
STEP 5: Link a Shared Library using target_link_libraries
target_link_libraries( hello_binary
PRIVATE
hello_library
)

Related

Converting simple makefile into CMakeLists.txt?

I'm trying to convert this simple makefile into a CMakeLists.txt. I'm specifically struggling with the flags.
SOURCE = triangle.cpp shader.cpp
CC = g++
CFLAGS = -Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw
OBJECT = window
default:
$(CC) -o $(OBJECT) $(SOURCE) $(CFLAGS)
clean:
rm -f $(OBJECT)
Here's my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Graphics)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw")
#add_definitions(${CMAKE_CXX_FLAGS})
add_executable(
trtangle
triangle.cpp
)
Here's the error message I get:
Scanning dependencies of target triangle
[ 50%] Building CXX object CMakeFiles/triangle.dir/triangle.cpp.o
clang: warning: -framework OpenGL: 'linker' input unused [-Wunused command-line-argument]
clang: warning: -framework GLUT: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lGLEW: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lglfw: 'linker' input unused [-Wunused-command-line-argument]
/Users/neilculbertson/Desktop/OpenGL/triangle.cpp:2:10: fatal error: 'GL/glew.h' file not found
#include <GL/glew.h>
^~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/triangle.dir/triangle.cpp.o] Error 1
make[1]: *** [CMakeFiles/triangle.dir/all] Error 2
make: *** [all] Error 2
I've also tried setting CMAKE_C_FLAGS But still no luck. Keep in mind when I use the makefile alone, everything compiles and works perfectly. I'm wondering if it's a GL error? or maybe I installed GLEW weirdly?
cmake
cmake_minimum_required(VERSION 3.0) # This line is required
project(triangle) # This line is required
make
CC = g++
CFLAGS = -Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw
cmake
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw")
make
SOURCE = triangle.cpp shader.cpp
default:
$(CC) -o $(OBJECT) $(SOURCE) $(CFLAGS)
cmake
# The name of the result and all required sources
add_executable(triangle triangle.cpp shader.cpp)
# These libraries are required
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 3.0 REQUIRED)
# ... and the path to the header files and so files
target_include_directories(triangle ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(triangle ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} glfw3)
Other possibility is to collect all sources within a list at first.
set(SOURCES triangle.cpp shader.cpp)
add_executable(triangle ${SOURCES)
make
OBJECT = window
cmake
Not necessary
make
clean:
rm -f $(OBJECT)
cmake
This is built in.
cmake_minimum_required(VERSION 3.0)
project(Graphics)
add_executable(
trtangle
triangle.cpp
)
target_compile_options(trtangle PRIVATE -Wall -Wextra -pedantic)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 3.3 REQUIRED)
target_include_directories(trtangle ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(trtangle ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} glfw)

How to add external library to CMakeLists?

Normal compilation (works fine):
g++ DBHandler.cpp Functions.cpp Main.cpp -I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq -o dbhandler
It`s possible to run:
./dbhandler
CMakeLists.txt (standard):
cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler config++ pqxx pq)
add_executable(dbhandler ${SOURCES})
How to change CMakeLists.txt and add:
-I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq
to compile program using cmake?
The correct solution is to use the CMake package PkgConfig to use pkg_search_module
Your file will become:
cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)
find_package(PkgConfig REQUIRED)
pkg_search_module(CONFIGPP REQUIRED config++)
pkg_search_module(PQ REQUIRED pq)
pkg_search_module(PQXX REQUIRED pqxx)
include_directories(include ${CONFIGPP_INCLUDE_DIRS} ${PQ_INCLUDE_DIRS} ${PQXX_INCLUDE_DIRS})
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler ${CONFIGPP_LIBRARIES} ${PQ_LIBRARIES} ${PQXX_LIBRARIES})
add_executable(dbhandler ${SOURCES})

Xcode 7.3 + linking to relative paths = ld: file not found: ../lib/lib_.dylib

I have a project which compiles fine on Xcode 7.2 and earlier, but gets linker errors when compiling with Xcode 7.3. I've been able to narrow it down to a simple example project that duplicates the problem.
The basic project structure is like this:
.
├── one
│   ├── lib
│   │   └── lib1.dylib
│   └── src
│   └── one.c
├── two
│   ├── lib
│   │   └── lib2.dylib
│   └── src
│   └── three.c
└── three
├── lib
   │   └── lib3.dylib
└── src
└── two.c
lib3 links with lib2, and lib2 links with lib1. When the library is compiled, its output is in the ../lib directory relative to its src directory.
When I try to compile lib3, it errors with file not found: ../lib/lib1.dylib, even though ../../one/lib is in the library search path.
What strange and mystical linker flag can I use to get the search paths to work out okay? Or is this a bug with Xcode?
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C one/src
cc -I ../../include -c -o one.o one.c
cc -dynamiclib -undefined dynamic_lookup -flat_namespace -o ../lib/lib1.dylib one.o
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C two/src
cc -I ../../include -c -o two.o two.c
cc -dynamiclib -undefined dynamic_lookup -flat_namespace -L ../../one/lib -o ../lib/lib2.dylib two.o -l1
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C three/src
cc -I ../../include -c -o three.o three.c
cc -dynamiclib -undefined dynamic_lookup -flat_namespace -L ../../two/lib -L ../../one/lib -o ../lib/lib3.dylib three.o -l2
ld: file not found: ../lib/lib1.dylib for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [../lib/lib3.dylib] Error 1
make: *** [all] Error 2
OK it took me a while to fix this error. I knew that part of the issue was setting the runpath and the install name of the libraries, so they could be found during linking and at runtime, which would be the next issue you faced.
I settled for building the libraries into a common top-level lib directory, however at the end of the day I'm pretty sure that actually wasn't necessary, but it did cut down on command line options being passed to the linker which is very cumbersome as it needs to be done with -Wl,-linker-option and -Wl,linker-option-argument as you are using cc for linking rather than ld directly.
I think the actual answer is to add -l1 to your three/src/Makefile line:
$(LD) -o $# $< -l2 -l1
It's not clear to me why this was necessary as if lib3 depends on lib2 then it shouldn't be necessary to add lib2's dependencies into the linker line. It's perhaps something to do with your use of -undefined dynamic_lookup, as I've never seen that option used before. However I didn't have time to experiment fully with this.
If you make this change to your version of the Makefiles you will likely have runtime issues as the runpath and install name are implied from whatever you passed to -L (which was ../../one/lib and ../../two/lib and they will almost certainly be wrong at runtime). Therefore it's better to explicitly set the runpath and install name, and every library had their runpath set to #loader_path/ and their install name set to #rpath/libX.dylib. The executable that uses these libraries will likely want a different runpath in order to find the libraries.
See my pull request for the changes I made.
I would like to commend you on how you presented this question with a testable project hosted on github; without that you would be unlikely to get any decent feedback.

Impossible to link SDL/GLUT/GLM Ubuntu [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I actualy work on a openGL projet on my VM but i need openGL 4.5 so i have install ubuntu on my laptop with (GTX 870M) who is compatible 4.5 (i check with glxinfo). But my problem is after install gcc, build-essential, libglew-dev, freeglut3-dev, freeglut3 and SDL2. I can't make my projet i have error like undefined reference on « SDL_WasInit » , « glBegin », .... for all library installed...
i try with makefile like :
ifeq "$(shell uname)" "Darwin"
LIBGL= -framework OpenGL
else
LIBGL= -lGLU -lGL
endif
CXXFLAGS += `pkg-config glew --cflags` `sdl2-config --cflags` -g -W -Wall -Wno-unused-parameter -Wno-deprecated-declarations
LDFLAGS += `pkg-config glew --libs` `sdl2-config --libs` $(LIBGL)
all : main.exe
run : main.exe
./main.exe
main.exe : main.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o$# main.cpp
sol : main_solution.exe
runs : main_solution.exe
./main_solution.exe
main_solution.exe : main_solution.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# main_solution.cpp
clean :
rm -f *.o *.exe
and with cmakefile like :
cmake_minimum_required(VERSION 3.3)
project(src)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lglut -lGLU -lGL -lGLEW -lm -lSDL2 -lSDL2main -Wall -g")
set(SOURCE_FILES
"""ALL SOURCE FILES"""")
add_executable(src ${SOURCE_FILES})
This projet (cmakefile and makefile) work fine on my virtual machine ...
I hope you can help me thx.
In CMake you use target_link_libraries to specify which libraries to use. In general you want to use find_package(…) to locate the configuration for a specific library, then use the variables introduced by it to link.
find_package(OpenGL)
add_executable(foo …)
target_link_libraries(foo ${OPENGL_gl_LIBRARY} …)
You should look into the cmake modules that are used by find_package to see what the names of the variables they configure are.

undefined reference to `lzma_code'

I try to pack my application and static link all libraries. But I get this error.The Makefile is shown below:
CC = gcc
INCPATH = -I/home/johnny/Application/FileBasedReG/include/RealityGrid
LIBS = -L/home/johnny/Application/FileBasedReG/lib/RealityGrid -lReG_Steer -l:libxml2.a -l:libncurses.a -l:libm.a -l:libz.a -l:libtermcap.a
OBJECTS = mini_steerer.o
TARGET = mini_steerer
###### Compile ######
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(INCPATH) -o $(TARGET) $(OBJECTS) $(LIBS)
mini_steerer.o: ./mini_steerer.c ./mini_steerer.h
$(CC) -c $(INCPATH) -o mini_steerer.o ./mini_steerer.c
I think I need to add one or two more static libraries, but I can't find what they are.
On a debian / ubuntu system, apt-get install liblzma-dev will do the trick. Link with -llzma.
You're probably missing the liblzma.a, which should have come as a dependency from libxml2, if you had got it from a package.

Resources