Using boost in program created by g++


 
Thread Tools Search this Thread
Top Forums Programming Using boost in program created by g++
# 1  
Old 12-27-2012
Using boost in program created by g++

I am trying to use the split provided by boost.

I have a string and want to split on "/".

Code:
string  value = "trig/4";

Have no idea how to compile and link it. I have extracted boost in

Code:
/chrisd/tomso-12.04/source/library/boost_1_52_0

In my program /raytrac.cc I have put

Code:
#include <../../library/boost_1_52_0/boost/algorithm/string.hpp>

Code:
C++ = g++
OPT = -traditional -Wno-non-template-friend -Wno-deprecated -O3 -DNDEBUG
IDIR = -Ilibrary

dir_raytrac = ./programs/raytrac
raytrac : $(dir_raytrac)/raytrac.cc $(dir_raytrac)/raytrac_doc.hh library
    $(C++) $(OPT) $(IDIR) -lboost_regex $(dir_raytrac)/raytrac.cc -o raytrac
    -mv raytrac ../bin

dirlib = ./library
library : $(dirlib)/common.hh    \
    $(dirlib)/clorfncs.hh    \
    $(dirlib)/vect2.hh       \
    $(dirlib)/vector.hh      \
    $(dirlib)/matrix.hh      \
    $(dirlib)/dynbaseobj.hh  \
    $(dirlib)/list.hh        \
    $(dirlib)/stack.hh       \
    $(dirlib)/tree.hh        \
    $(dirlib)/string.hh      \
    $(dirlib)/layer.hh       \
    $(dirlib)/linlay.hh      \
    $(dirlib)/velmod.hh


Last edited by kristinu; 12-27-2012 at 02:27 PM..
# 2  
Old 12-27-2012
Not sure it justifies the overhead of regex, maybe just find and split: Design Topics - 1.52.0 if not strchr() or http://www.cplusplus.com/reference/string/string/at/ !

Compiling and linking are just having the include dirs in -I's, a good dynamic search path or static lib dirs in -L's, libraries -lxxx and source or object files in the right order, and away you go. What happens when you compile?
# 3  
Old 12-27-2012
My makefile is in directory source. Running tree gives me.
My program resides in source/programs/raytrac

I really have no clue what I should do. Quite stuck.

Code:
cd source

tree  -d -L 3
.
├── fortran
│   ├── bin
│   │   ├── modBin
│   │   └── objBin
│   └── fmod
├── include
├── library
│   └── boost_1_52_0
│       ├── boost
│       ├── doc
│       ├── libs
│       ├── more
│       ├── status
│       └── tools
├── others
├── programs
│   ├── getmisfit
│   ├── getpdf
│   ├── getvel
│   ├── raytrac
│   ├── rdt
│   ├── surface
│   ├── tdarwin
│   ├── tracemisfit
│   ├── tracepdf
│   ├── tracepdf2d
│   └── tsimplex
└── scripts
    ├── awk
    ├── bash
    ├── html
    │   └── search
    ├── ksh
    │   └── TAG201011
    ├── latex
    ├── perl
    ├── python
    └── tcsh

I have done like this

Code:
g++ -traditional -Wno-non-template-friend -Wno-deprecated -O3 -DNDEBUG -Ilibrary ./programs/raytrac/raytrac.cc \
   -I./library/boost_1_52_0 -L./library/boost_1_52_0/libs -o raytrac

Ok , I seem to be getting something now

I am using

In the makefile I use

Code:
dir_raytrac = ./programs/raytrac
raytrac : $(dir_raytrac)/raytrac.cc $(dir_raytrac)/raytrac_doc.hh library
    $(C++) $(OPT) $(IDIR) $(dir_raytrac)/raytrac.cc \
    -I./library/boost_1_52_0 -L./library/boost_1_52_0/libs -o raytrac
    -mv raytrac ../bin

and in the program I use

Code:
  #include <boost/foreach.hpp>
  string hello ("Hello, world!");
  BOOST_FOREACH (char ch, hello) {
    cout << ch << endl;
  }
  cout << endl;


Last edited by kristinu; 12-27-2012 at 05:54 PM..
# 4  
Old 12-27-2012
Some prefer to do it in two lines, one for .cc.o and one for .o.exe or whatever, so the first also supports code going into libraries, and there is just one place for those options in make definitions.

I usually see the compiler options, -D's, -I's (preprocess is before compile, is before link), the -L right before the -l's it supports, -l's for each linked library and source or local object at the right end. I like to use dynamic linking, so no -L's, just a good LD_LIBRARY_PATH (or whatever your system likes to use, see man ld() for rules).

Sometimes I would -c compile all the .c or .cc into .o and ar them all into one library .a file, as order does not matter within a library, and finally compile with the .a file explicitly only. Only good for one main()! One best practice says put only one subroutine/function in each source file, and then the order is tricky. You can have one .a for each main() and it's "private" functions/methods/subroutines, and then link in any common code libraries.

The -I path must support the entry name or relative path in the source #include's (-I/a/b/c and #include "d/e.h" is for /a/b/c/d/e.h).

Last edited by DGPickett; 12-27-2012 at 06:07 PM..
# 5  
Old 12-27-2012
Can you give me some examples on how to do what you are saying?

---------- Post updated at 05:34 PM ---------- Previous update was at 05:28 PM ----------

I have done like this

Code:
C++ = g++
OPT = -traditional -Wno-non-template-friend -Wno-deprecated -O3 -DNDEBUG
IDIR = -Ilibrary
dir_raytrac = ./programs/raytrac
raytrac : $(dir_raytrac)/raytrac.cc $(dir_raytrac)/raytrac_doc.hh library
    $(C++) $(OPT) $(IDIR) $(dir_raytrac)/raytrac.cc \
    -I./library/boost_1_52_0 -L./library/boost_1_52_0/libs -o raytrac
    -mv raytrac ../bin

dirlib = ./library
library : $(dirlib)/common.hh    \
        $(dirlib)/clorfncs.hh    \
        $(dirlib)/vect2.hh       \
        $(dirlib)/vector.hh      \
        $(dirlib)/matrix.hh      \
        $(dirlib)/dynbaseobj.hh  \
        $(dirlib)/list.hh        \
        $(dirlib)/stack.hh       \
        $(dirlib)/tree.hh        \
        $(dirlib)/string.hh      \
        $(dirlib)/layer.hh       \
        $(dirlib)/linlay.hh     \
        $(dirlib)/velmod.hh

---------- Post updated at 05:57 PM ---------- Previous update was at 05:34 PM ----------

Ok , I think I got you.
# 6  
Old 01-01-2013
You haven't said what you want to do with the path processing. But you can use strchr() to get each directory or use strrchr() to get to the filename directly.

Regarding your build system, I think it's best to have a make file in each program directory, then have a single make file in the top level program directory that calls the others. You can do it like this:
Code:
SUBDIRS = getmisfit getpdf getvel raytrac rdt 

.PHONY: subdirs $(SUBDIRS)

subdirs: $(SUBDIRS)

$(SUBDIRS):
        $(MAKE) -C $@

See: GNU `make'

Also, GNU Make has a lot of built in rules. It already defines CC and CXX macros for the C and C++ compilers. Flags common to both compilers are set with CPPFLAGS and LDFLAGS for linker options.

As your set of programs probably all need common paths, you should set them in a separate make include file in the programs directory.

The raytrac make file will look something like:
Code:
include "../common.mk"

OBJS: raytrac

all: raytrac

clean:
        -rm raytrac *.o

raytrac: $(OBJS)
        $(LINK.cc) $^ -o $@
        cp $@ $(BIN)

Do that for each project.

common.mk has:
Code:
BIN = ../../bin/
CXXFLAGS += ../../library/boost_52_0/
LDFLAGS += -L ../../library/boost_52_0/libs -l <libname>

Clearly you don't need to specify Boost if you're not using it (by using the string routines instead).

LINKcc is GNU Make's C++ link rule. You can see all the rules by running:
Code:
make -p

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Boost.Test and CMake

Hi, I just started using CMake and the Boost Libraries. In this progress I encountered some problems. One of these problems is combining Boost unit tests with cmake. I don't know how to set the whole project up. I tried to set up a simple test project. This contains a main.cpp a comp.cpp and the... (0 Replies)
Discussion started by: ElCoyote
0 Replies

2. UNIX for Dummies Questions & Answers

How do I declare boost?

Hello all, I am trying to "make" a database system, VDB (Veritas Data Base), and when I run "make" I receive the following error: VDBException.h:19: error: expected `)' before '*' token VDBException.h:20: error: expected `)' before '*' token VDBException.h:43: error: expected `)' before '*'... (4 Replies)
Discussion started by: Tyler_92
4 Replies

3. Solaris

how to install boost 1_49_0 in solaris

hi guys, i downloaded boost1_49_0 tar.gz... then unzip and untar... how to install boost 1_49_0 in solaris plz help me (1 Reply)
Discussion started by: coolboys
1 Replies

4. Programming

Boost C++ ASIO Networking

Hi, Based on the following example in the Boost C++ website: www. boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp]doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp I tried to create a similar TCP server that waits to accept a client connection... (0 Replies)
Discussion started by: tanlccc
0 Replies

5. Solaris

boost thread not accessible to boost::move error

Hi All I am working unders Sun Solaris and I am not "/opt/boost/boost/thread/detail/thread.hpp", line 344: Error: boost::thread::thread(boost::thread&) is not accessible from boost::move(boost::detail::thread_move_t<boost::thread>). Do you know if there are other solutions other than... (2 Replies)
Discussion started by: manustone
2 Replies

6. Solaris

Sun Studio 10 + Boost 1.36

Is it possible to build Boost 1.35 using Sun Studio 10? I can build Boost 1.35 using Sun Studio 11 successful. However, i'm unable to build it using Sun Studio 10 using the exact method. I really apprecaite if any expert can help on this. Thanks, (2 Replies)
Discussion started by: shingpui
2 Replies

7. Linux

How do I boost the Linux performace

Hi All, I installed Linux recently on my PC and finding it difficult to boost its performance. It takes hell lot of time to open Mozilla, text pad , & even the booting process is too slow, many a times I got to manually power off to shutdown the computer. I will be glad if you could help me... (18 Replies)
Discussion started by: jayfriend
18 Replies
Login or Register to Ask a Question