In the old Makefile there is an include part:
SHELL=/bin/sh
CORE_SRC=\
./Core/allocator.cpp \
./Core/etc... \
CORE_OBJS=$(CORE_SRC:.cpp=.o)
INCLUDE=\
`pkg-config --cflags glib-2.0 libpng` \
`sdl-config --cflags` \
`freetype-config --cflags` \
`./python-config-linux.sh --cflags` \
-I./TopLayer -I./etc...
CC=g++-4.4
CFLAGS=-O3 -pipe -Wall -fPIC -D__STDC_CONSTANT_MACROS
CORE_LFLAGS=\
-fPIC \
-Wl,-rpath,./libs
CORE_LDLIBS=\
`pkg-config --libs glib-2.0 libpng` \
`sdl-config --libs` -lz -ljpeg \
`freetype-config --libs` \
`curl-config --libs` \
-L./$(LIBSDIR) \
-letc...
GAME_LFLAGS=\
-shared -pthread -fPIC \
-Wl,-rpath,../libs
GAME_LDLIBS=\
-lm \
`python-config-linux.sh --libs`
target_name: $(CORE_OBJS) $(CORE_NAME)
target_name: override CFLAGS += -DAV_OUTPUT
$(CORE_NAME): $(CORE_OBJS)
$(CC) $(CORE_LFLAGS) $(CORE_OBJS) $(CORE_LDLIBS) -o $#
.c.o:
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $*.o
.cpp.o:
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $#
The execution of those configs is:
$pkg-config --cflags glib-2.0 libpng
-> -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/libpng12
$sdl-config --cflags
-> -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT
$freetype-config --cflags
-> -I/usr/include/freetype2
$./python-config-linux.sh --cflags
-> -I/python-2.7.10/include/python2.7 -I/python-2.7.10/include/python2.7 -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
I don't understand and a bit confused about Cmake - should I use target_include_dirictories() or find_packages()?
Should target_include_dirictories() be used also with --cflags and libs like glib-2.0 libpng for pkg-config?
*Updated makefile. Removed unnecessary targets etc.
You will need both find_package() and target_link_libraries().
target_include_directories() is almost always obsolete when using libraries found through find_package. Docs.
Example
Example tested on macOS HighSierra with CMake 3.12.
project(Example)
cmake_minimum_required(VERSION 3.10)
find_package(PNG REQUIRED)
add_executable(prog main.c) # main.c from: http://zarb.org/~gc/html/libpng.html
target_link_libraries(prog PNG)
# On Ubuntu 14.04 + CMake 3.2 use the older syntax (FindPNG.cmake is too old) from the next line:
# target_link_libraries(prog ${PNG_LIBRARIES})
Related
How would I make a DLL file from a Makefile where the files are written in Fortran?
I'm working with Windows but having it compiled under a Linux system isn't a problem.
I have two modules and one file that uses those modules.
Normally, I would just write the following to to produce what I want:
gfortran module1.f90 module2.f90 main.f90 -o main.dll
If module1.f90 and module2.f90 are just modules and you want to create a dll, you may use the following command:
gfortran -o main.dll module1.f90 module2.f90 -shared -fPIC -lgfortran
It will generate a dll that can be loaded later by the main program.
The main flags here for generate a lib are -shared and -fPIC.
If main.f90 is the main program, the output should be an exe, and the command to compile maybe:
gfortran -o main.exe module1.f90 module2.f90 main.f90
It will generate an exe with the main file using the modules.
Edit:
Here, is a sample Makefile that builds the DLL (for the question):
FC=gfortran
FFLAGS=-g -shared -fPIC
LDFLAGS=-lgfortran
main.dll: main.f90 module1.o module2.o
$(FC) $(LDFLAGS) -o $# $?
module1.o: module1.f90
$(FC) $(FFLAGS) -o $# $?
module2.o: module2.f90
$(FC) $(FFLAGS) -o $# $?
clean:
rm -f *.o *.exe *.dll
After executing it:
gfortran -g -shared -fPIC -o module1.o module1.f90
gfortran -g -shared -fPIC -o module2.o module2.f90
gfortran -o main.dll main.f90 module1.o module2.o
It generates the DLL:
20/12/2016 16:23 59.091 main.dll
Maybe, this Makefile can be improved with use of macros.
HTH
tested with gfortran version 6.2.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
Let's say I want to rewrite this:
main.o: main.cpp
g++ -c main.cpp
factorial.o: factorial.cpp
g++ -c factorial.cpp
hello.o: hello.cpp
g++ -c hello.cpp
in a more generic way as something like this:
SOURCES = main factorial hello
$(SOURCES).o: $(SOURCES).cpp
g++ -c $(SOURCES).cpp
How do I do that?
First we tell the compiler how to nme the output files, just to avoid misunderstanding:
main.o: main.cpp
g++ -c main.cpp -o main.o
factorial.o: factorial.cpp
g++ -c factorial.cpp -o factorial.o
hello.o: hello.cpp
g++ -c hello.cpp -o hello.o
Then we put in automatic variables, to reduce redundancy:
main.o: main.cpp
g++ -c $< -o $#
factorial.o: factorial.cpp
g++ -c $< -o $#
hello.o: hello.cpp
g++ -c $< -o $#
Then we realize that these rules all look the same, so we combine them as a static pattern rule:
main.o factorial.o hello.o: %.o : %.cpp
g++ -c $< -o $#
Then we use a variable to store the names of the objects:
OBJECTS := main.o factorial.o hello.o
$(OBJECTS): %.o : %.cpp
g++ -c $< -o $#
im try to change my webrick to passenger with nginx but when i try
passenger-install-nginx-module
seems everything is alright at first but at the end it comes with this error i tried to Google the problem but some of the post are
unanswered very much appreciated for the helping thanks to all
# sh ./configure --prefix='/opt/nginx' --with-http_ssl_module --add-module='/home/led/.rvm/gems/ruby-1.9.3-rc1/gems/passenger-3.0.0.pre4/ext/nginx'
checking for OS
+ Linux 2.6.42.12-1.fc15.i686 i686
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)
checking for gcc -pipe switch ... found
checking for gcc variadic macros ... found
checking for C99 variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
configuring additional modules
adding module in /home/led/.rvm/gems/ruby-1.9.3-rc1/gems/passenger-3.0.0.pre4/ext/nginx
checking for Math library ... found
+ ngx_http_passenger_module was configured
checking for PCRE library ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile
checking for int size ... 4 bytes
checking for long size ... 4 bytes
checking for long long size ... 8 bytes
checking for void * size ... 4 bytes
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system endianess ... little endianess
checking for size_t size ... 4 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 4 bytes
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for strerror_r() ... found but is not working
checking for gnu style strerror_r() ... found
checking for sys_errlist[] ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1 library is not used
+ using system zlib library
nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/logs/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
# make
make -f objs/Makefile
make[1]: Entering directory `/tmp/root-passenger-5241/nginx-0.7.67'
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_log.o \
src/core/ngx_log.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_palloc.o \
src/core/ngx_palloc.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_array.o \
src/core/ngx_array.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_list.o \
src/core/ngx_list.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_hash.o \
src/core/ngx_hash.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_buf.o \
src/core/ngx_buf.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_queue.o \
src/core/ngx_queue.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_output_chain.o \
src/core/ngx_output_chain.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_string.o \
src/core/ngx_string.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_parse.o \
src/core/ngx_parse.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_inet.o \
src/core/ngx_inet.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_file.o \
src/core/ngx_file.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_crc32.o \
src/core/ngx_crc32.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_rbtree.o \
src/core/ngx_rbtree.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_radix_tree.o \
src/core/ngx_radix_tree.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_slab.o \
src/core/ngx_slab.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_times.o \
src/core/ngx_times.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_shmtx.o \
src/core/ngx_shmtx.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_connection.o \
src/core/ngx_connection.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_cycle.o \
src/core/ngx_cycle.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_spinlock.o \
src/core/ngx_spinlock.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_cpuinfo.o \
src/core/ngx_cpuinfo.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_conf_file.o \
src/core/ngx_conf_file.c
gcc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_resolver.o \
src/core/ngx_resolver.c
src/core/ngx_resolver.c: In function ‘ngx_resolver_process_ptr’:
src/core/ngx_resolver.c:1422:32: error: variable ‘qclass’ set but not used [-Werror=unused-but-set-variable]
src/core/ngx_resolver.c:1422:25: error: variable ‘qtype’ set but not used [-Werror=unused-but-set-variable]
cc1: all warnings being treated as errors
make[1]: *** [objs/src/core/ngx_resolver.o] Error 1
make[1]: Leaving directory `/tmp/root-passenger-5241/nginx-0.7.67'
make: *** [build] Error 2
You're using v3.0.0.pre4 of the passenger gem, which was release in September of 2010.
Passenger is now at v3.0.11. I'd suggest updating passenger: gem update passenger and running passenger-install-nginx-module again.
My make file has worked, up until I tried some variable substitution. %.o is not recognized.
I get make: * No rule to make target `%.o', needed by `parser'. Stop.
CC=gcc
CFLAGS=-ansi -pedantic -Wall -ggdb3
PROJECT=project.c project.h
PARSER=parser.c parser.h
OBJ=project.o parser.o
#CFILE=project.c parser.c
#1 no problem
#parser: project.o parser.o
# $(CC) $(CFLAGS) -o $# $^
#2 no problem
#parser: $(OBJ)
# $(CC) $(CFLAGS) -o $# $^
#3 this fails
parser: %.o
$(CC) $(CFLAGS) -o $# $^
#parser: project.o parser.o
# gcc -ansi -pedantic -Wall -ggdb -o parser project.o parser.o
project.o: $(PROJECT)
$(CC) $(CFLAGS) -c $^
parser.o: $(PARSER)
$(CC) $(CFLAGS) -c $^
clean:
rm -f $(OBJ) parser
You're not creating a pattern rule, since there is no % in the target name. As a result the % has no special meaning in the list of dependencies. It's seen as a literal part of a file name; you can't use it as a regular wildcard.
In my makefile I have an object variable. I need to prepend obj/ to every .o file. How would I do this?
CC=g++
CFLAGS=-C -Wall
LDFLAGS=-lsqlite3 -lpthread -ldl
SOURCES=main.cpp Database.cpp actionInit.cpp TileSet.cpp Player.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=mahjong-counter
all: bin $(OBJECTS) $(EXECUTABLE)
bin:
mkdir -p bin
%.o: %.cpp
$(CC) $(LDFLAGS) $< -c -o $#
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $(EXECUTABLE)
clean:
rm $(OBJECTS)
You want CXX, not CC. CC is for C compiler, not the C++ compiler
In any case, I believe the following should work:
CXX=g++
CXXFLAGS=-C -Wall
LDFLAGS=-lsqlite3 -lpthread -ldl
OBJ_DIR = obj
BIN_DIR = bin
EXECUTABLE=mahjong-counter
SOURCES= main.cpp Database.cpp actionInit.cpp TileSet.cpp Player.cpp
OBJECTS= $(SOURCES:%.cpp=$(OBJ_DIR)/%.o)
all: dirs $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $(EXECUTABLE)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) $< -o $#
dirs:
mkdir -p $(BIN_DIR)
mkdir -p $(OBJ_DIR)
.PHONY: dirs all
You could use more expressive version of substitution you employed when assigning OBJECTS
OBJECTS=$(SOURCES:%.cpp=obj/%.o)
or use a standard text transformation function
OBJECTS=$(addprefix obj/,$(SOURCES:.cpp=.o))