开发者

#include boost without using libs linux

The problem: At the moment im compiling on Ubuntu but my server is running Fedora/Redhat. Ubunutu uses boost 1.42 and linux latest at this very moment is 1.41. So what i decided was to download the boost lib and put it in the folder of my workspace

Here is the directory structure

/workspace
    /myprogram
        /src
            /main.cpp
        /Debug
            /main
    /boost_1_42_0
        /downloaded from boost.com

In my main.cpp, i have this code

#include "../../boost_1_42_0/boost/regex.hpp"

Is this even posible or am i barking up the wrong tree. I have tried to compile it but it failed (ofcourse) with 13 errors

If i missed some information please ask for it, il try providing it

Make File (My Program is called vlogd)

################################################################################
# Automatically-generated file. Do not edit!
################################################################################

-include ../makefile.init

RM := rm -rf

# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include src/subdir.mk
-include src/class/vException/subdir.mk
-include src/class/mysqlcppapi/subdir.mk
-include src/class/mysqlcppapi/row/subdir.mk
-include src/class/mysqlcppapi/query_results/subdir.mk
-include src/class/mysqlcppapi/query/subdir.mk
-include src/class/mysqlcppapi/fields/subdir.mk
-include src/class/mysqlcppapi/exceptions/subdir.mk
-include src/class/mysqlcppapi/datetime/subdir.mk
-include objects.mk

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
endif

-include ../makefile.defs

# Add inputs and o开发者_JAVA百科utputs from these tool invocations to the build variables 

# All Target
all: vlogd

# Tool invocations
vlogd: $(OBJS) $(USER_OBJS)
    @echo 'Building target: $@'
    @echo 'Invoking: GCC C++ Linker'
    g++ -L/usr/lib64/mysql -L../../boost_1_42_0/lib/ -o"vlogd" $(OBJS) $(USER_OBJS) $(LIBS)
    @echo 'Finished building target: $@'
    @echo ' '

#     Other Targets
clean:
    -$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) vlogd
    -@echo ' '

.PHONY: all clean dependents
.SECONDARY:

-include ../makefile.targets

Object File

################################################################################
# Automatically-generated file. Do not edit!
################################################################################

USER_OBJS :=

LIBS := -lmysqlclient -lboost_regex


No need to include using full path, if you use gcc just specify the correct include path and link path

gcc -I../../boost_1_42_0/ myprogram.cpp -L../../boost_1_42_0/lib -lboostXYZ

Thus, all #include <boost/...> headers and libs will be first searched in your local boost.

Edit
Following the question in the comment.

By default -l will search for .so libraries. So if boost is built with e.g. libboost_regex.so and liboost_regex.a, then by default you will link to the .so. If you're linked to .so, on the working server you need to have correct versions of these libraries (several boost versions can be installed).

If you want to link implicitly to the static versions, either use the full path

gcc .... ../../boost_1_42_0/lib/libboost_regex.a

or

gcc ... -Wl,-Bstatic -L../../boost_1_42_0/lib -lboost_regex -Wl,-Bdynamic

or (in newer versions of ld)

gcc ... -L../../boost_1_42_0/lib -l:libboost_regex.a

Having the binary with ldd command you may see its shared library dependencies and check if boost ones are among them

ldd ./yourapp


I nice way to build boost libraries in your apps is to build your apps with bjam (with all its cross-platform friendliness)

I solve your problem like so - you can look here general documentation http://www.boost.org/doc/tools/build/doc/html/index.html

In my jamroot.jam (at the base of my project) - your /workspace folder- I have

#Name this location $(TOP),
path-constant TOP : . ;
## The location of the boost library
path-constant BOOST_BASE : ($TOP)/boost_1_42_0 ;  

#Define an alias to the regex libray
alias regex-library     
      : $(BOOST_BASE)/libs/regex/build//boost_regex
      : <include>$(BOOST_BASE) ;


# build your project file
build-project ./myprogram/build ;

Then in the myprogram/build directory I have a file called Jamfile.v2

project myprogram
  : source-location ../src  #your src directory
  : requirements <include>../src #your headerfiles can be in a different place if you wish 
             <warnings>all 
  : default-build   <threading>multi # for example
  : build-dir $(TOP)/build #where you want your object files put
;

exe myprogram   : main.cpp  
                 $(TOP)//regex-library # bjam should sort out the rest
         :
     <include> # any other includes
    ; 

alias install : install-bin ; 
# where you want your program installed
install install-bin : myprogram : <location>$(TOP)/bin ; 

Hope this helps. I have copied this out of my files so hopefully they should work - however there is a chance I made a mistake when putting your directory structure into them. Let me know if it doesn't work.

The advantage of this system is that you actually build the library - so all compiler flags will be correct. Also, if you upgrade the library, you should just need to change the BOOST_BASE flag. Also, the build should work on windows and linux without any modification....

All the best,

Tom

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜