With Eclipse: How to add include paths and libraries for all your C/C++ project

后端 未结 8 1894
闹比i
闹比i 2020-12-17 09:33

Is it possible to add include paths and libraries to all C/C++ projects? In others words: How can I make them global or copy one C/C++ project build setting to another one?<

相关标签:
8条回答
  • 2020-12-17 09:39

    For libraries I do the following in the source project:

    Project -> properties -> c/c++Build -> c++ linker -> libraries

    After this I select libraries (shift + mouse) & copy (ctrl + c)

    In the blank project:

    Project -> properties -> c/c++Build -> c++ linker -> libraries

    ctrl + v (paste)

    Works fine with eclipse juno

    0 讨论(0)
  • 2020-12-17 09:39

    I am looking into it long ago. A few posts mentioned one method: Project > Properties > C/C++ General > Paths and Symbols Then click Export Settings.. But it only works for including header files.

    To specify libraries and library paths, as far as I know, I do not find an easy solution. For example, I am now using ffmpeg libraries. If you use "Makefile", the including and library files can be expressed below:

    FFMPEG_LIBS= libavdevice
    libavformat
    libavfilter
    libavcodec
    libswresample
    libswscale
    libavutil
    CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS) LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)

    There are many files here. If I include them one by one in an Eclipse C++ project, it is daunting. I do not know why there is no option in Eclipse that we could include library files by using past a long line using ; as a separator, such as:

    -lavdevice -lavfilter -lswresample -lswscale -lavformat -lavcodec -ldl -lasound -lSDL -lz -lrt -lavutil -lm

    Is there any such way, or I have to type the library files one by one, or have to use Makefile?

    0 讨论(0)
  • 2020-12-17 09:44

    I faced the same problem and solved it like this : I saw that in the auto-generated makefile there are lines:

    -include ../makefile.init
     ...
    -include ../makefile.defs
     ...
    -include ../makefile.targets
    

    after reading How can I parameterize an Eclipse generated make file I realized that you can define variables outside of you project. That mean it can be shared variable to several projects. So I added makefile.defs file outside of the project and declare in it some variables:

    MY_INCLUDE = <path to include>...
    MY_LIBS_PATH = <path to libs>...
    MY_LIBS = -lmylib ... (the libs)
    

    than in all my projects "properties->c/c++ build->build variables" I defined new variable:

    my_include = $(MY_INCLUDE)
    my_libs_path = $(MYLIBS_PATH)
    my_libs = $(MY_LIBS)
    

    And than you can you those variables in the projects. Go to properties->c/c++ build->Settings and choose compiler includes and add $(my_include). Go to linker and choose Libraries and add to search path $(my_libs_path). For some reason the Libraries (-l) can't take my defined env variable so I bypass it by choosing linker and than go to Miscellaneous and add to linker flags $(my_libs). Finally you have to change the order in the command line (when the linker execute) cause the list of libs need to come after the .o files. So to do that you go to linker and tab and there you can see the final command line that is going to be executed. In "expert settings command line pattern" you move the ${FLAGS} to the back of the command line (that moves the $(my_libs) string to be in the back of the command line). THAT ALL. its a lot but you need to do it only once... hope that it helps someone...

    0 讨论(0)
  • 2020-12-17 09:48

    Yes, you can, in 2 steps, more user-friendly as the one mentioned by parvus. Both are done in "Project Properties" -> "C/C++ General" -> "Paths and Symbols":

    1. In an existing project where you've already configured the include paths: Highlight the include paths (in the tab "Includes") which you want to copy to the new project and choose "Export". Note that this adds an [Exp] tag to each line.
    2. In the new created project: In the "References" tab, tick the checkbox of the just mentioned project and when you switch back to the "Includes" tab, you see that all the paths you've just exported appear there.

    Referencing the libraries works the same way.

    Also have a look at:

    • the Eclipse Juno Documentation to this topic,
    • a related StackOverflow question
    0 讨论(0)
  • 2020-12-17 09:55

    I often use the CPATH environment variable to include different directories across all my projects. It's found under: c/c++->build->environment. Be sure to separate each path with a ':' character (not a ';' semicolon).

    You can also use PATH to include static libs. However, for me it's only worked reliably in linux. In OSX it ruins make.

    The alternative would be to add these libraries and headers to your environments default search paths; place likes /usr/local/lib. Read up on where your linker searches by default.

    0 讨论(0)
  • 2020-12-17 10:00

    I just copy one project from using eclipse built in Project explorer. Afterwards, just rename source files.

    0 讨论(0)
提交回复
热议问题