I used to use Xcode to build and run C++ program.
I use command line to compile the same source code in my Xcode project.
Compiling a individual .cpp file is OK.
Compiling more complicated project(more than one file) is NOT OK.
I have tried gcc, g++, clang, clang++.
The main problem is undefined symbol.
Could you show how to compile complicated project (more than one file) by command line?
From your description, it sounds like you're not using the -c (compile, but don't link) flag. Steps to build your project:
Compile all of the source files:
c++ -c file1.cpp
c++ -c file2.cpp
c++ -c file3.cpp
Link your final executable:
c++ file1.o file2.o file3.o
You can use an optional -o flag to specify the output program name. It probably defaults to a.out.
It would be better to use some kind of build tool (like make, for example) to automate this process, or you'll find it to be very error prone.
Related
I have been trying to compile a large Fortran code with many files in it on a HPC with the use of CMake. CMake properly configures and generates a Makefile. While Making, I get an error saying '/bin/sh: ftn: command not found'. The Makefile tries to compile the code like this->
ftn -o CMakeFiles/s3d.x.dir/modules/param_m.f90.o
When I compile it on my personal system or another HPC, it goes like->
mpif90 -o CMakeFiles/s3d.x.dir/modules/param_m.f90.o
I don't know why CMake is not able to put actual compilers in place of 'ftn'.
I would really appreciate if any suggestions come up.
I want to create a static library libmylib.a from mylib.c/.h and link it to a project to use this library in bootloader code using the arm-none-eabi-gcc cross compiler in ubuntu 20.04 LTS.
I have an electronic engineering background, so I'm kind of new in this compiler and linker stuff.
What I know:
I've been searching about this, and found out that '.a' are just packed '.o' files, and that's it. You can do it using ar in linux. I don't know how to manage the dependencies for this '.a' file, for example, or how to link it to the project.
What I want to know:
I really want to understand how it works, to compile and generate the bin, elf or hex files using these static libraries for arm using the arm-none-eabi-gcc cross compiler (found some for linux), but I don't know how to search for this properly, how to learn it in a linear way. If you guys could help me on this I would be really grateful.
First you create your library objects. Let us say that you have a foo function written in foo.c, then you do:
arm-none-eabi-gcc -c foo.c
The -c options tells the compiler to stop after assembling and no go further.
Then you need to create the .a file
arm-none-eabi-ar -rc libfoo.a foo.o
this command creates a static library called libfoo.a
At the end you compile your main with:
arm-none-eabi-gcc -L. -lfoo main.c -o main
Note that in -l flag we don put "lib" and ".a", those are automagically added. The -L. flag tells gcc to look into the current folder for library files.
I have a CortexM0 project using a custom Makefile that builds and debugs successfully on a 1st machine.
Now trying to move the project to a second Mac.
Same version of Eclipse.
On build I get a linker error:
EclipseApr2019/gcc-arm-none-eabi-5_2-2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/bin/ld: cannot find -lg
My make file looks like this (extract):
# echo "path="$(TOOLS)
$(TOOLS)arm-none-eabi-gcc -n -v -mcpu=cortex-m0 -mthumb -g -nostartfiles -T STM32F031C6_simple.ld main.c StartUp_simple.s -o $(NAME).elf
I have tried to append the ARM gcc tools directory to the PATH variable in the Project, but no luck.
I would add a -l option to the link stage in the makefile, but do not know why this library is being pulled in or where it is. My code only does a series of shifts and reads/writes to registers on an MCU. The build on the 1st machine worked fine without specifying a library location like this.
Given I have custom makefile and am not generating Makefile automatically, there are no tool settings (and Library search path) available under Properties/CC++Build/Settings.
What is library "g" that the linker is pulling in?
Where is it?
Under Eclipse, how can I point the linker to the library?
Why didn't I need to do that before?
What is some general advice for designing an Eclipse project with a custom makefile to make it most portable between machines?
Thank you.
Eclipse IDE for C/C++ Developers
Version: 2019-03 (4.11.0)
I try to use the c++ language bindings for the ev3dev lego brick: https://github.com/ddemidov/ev3dev-lang-cpp
The instruction is as follows:
mkdir build
cd build
cmake .. -DEV3DEV_PLATFORM=EV3
make
I am running windows and have cmake and mingw available. After running cmake it creates some files in the build directory. However: There is no makefile which could be picked of by make. So I am wondering how iam supposed to compile these bindings
On Windows, CMake generates a MSVC solution by default. Check for a .sln file in your build directory.
The instructions you linked are assuming a Unix-ish platform, where the default is to create Makefiles.
If you actually want Makefiles on Windows, add -G "Unix Makefiles" to the cmake line.
If you want to use MSVC as compiler but work on the command line, another option is -G "NMake Makefiles", and calling nmake after that.
Make sure to delete your build directory before trying to build a new generator target. CMake can be touchy about that.
Check cmake --help for a list of available options. (Especially the generator targets are platform-specific.)
I have a problem with compiling a fortran program with the gfortan complier.
The main program is located in main.f. So, I write in console:
gfortran D:\test\test.f
But it displays me a lot of errors such as:
C:\Users\Efds~1\AppData\Local\Temp\cchFNGgc.o:test.f:<.test+0x3a>: undefined reference to '_gridw_'
C:\Users\Efds~1\AppData\Local\Temp\cchFNGgc.o:test.f:<.test+0x3a>: undefined reference to '_gridz_'
etc.
I think it's because of functions gridw, gridz etc. are located in other *.f files. But I don't know how to link these all together.
Also, I tried to use Compaq Visual Fortran Complier, but it didn't help me.
A basic command for compiling and linking multiple source files into one executable would be
gfortran -o executable source1.f source2.f source3.f
taking care that any .f file you specify is named to the right of any other source files on which it depends. All of this, and much more besides, is well covered in the compiler's documentation.
As noted above, you can compile several files with the same command, but it's quite unusual.
You may prefer first compile to object files (".o") :
gfortran -c gridw.f
gfortran -c gridz.f
And then compile the program
gfortran test.f grodw.o gridz.o
If you have many files to link, it may be interesting to build a library:
ar cru mylib.a gridw.o gridz.o
gfortran test.f mylib.a
If you name your library libSOMETHING.a, you can simply write
gfortran test.f -lSOMETHING